Skip to content

TYP: enforce annotation on SingleBlockManager.__init__ #32421

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Mar 11, 2020

Conversation

jbrockmendel
Copy link
Member

cc @simonjayhawkins my impression from yesterday's threads was that mypy should be catching the wrong-type being passed in SingleBlockManager.__init__, did I misunderstand something?

Also (and I know ive asked before) is there a nice way to annotate the return type as "same type as self"? I tried pandas._typing.T but that didnt do it.

Parts of this are repeated in several BlockManager methods, should probably be made into a helper function, pending discussion on above.

@jreback jreback added the Typing type annotations, mypy/pyright type checking label Mar 4, 2020
@@ -154,6 +154,7 @@ def make_empty(self, axes=None) -> "BlockManager":
if self.ndim == 1:
assert isinstance(self, SingleBlockManager) # for mypy
blocks = np.array([], dtype=self.array_dtype)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

blocks -> arr

bm = type(self)(result_blocks, self.axes, do_integrity_check=False)
if self.ndim == 1:
assert len(result_blocks) == 1
bm = type(self)(result_blocks[0], self.axes) # type: ignore
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why are you splitting this constructions? much harder to grok this code

@@ -626,8 +631,12 @@ def comp(s, regex=False):
rb = new_rb
result_blocks.extend(rb)

bm = type(self)(result_blocks, self.axes)
bm._consolidate_inplace()
if self.ndim == 1:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i would make a function or method to do this, you seem to be repeating this code

@jreback jreback added the Internals Related to non-user accessible pandas implementation label Mar 4, 2020
@simonjayhawkins
Copy link
Member

sorry @jbrockmendel didn't get round to this today. will look tomorrow.

@@ -149,6 +151,13 @@ def __init__(
self._blknos = None
self._blklocs = None

@classmethod
def _from_blocks(cls, blocks: List[Block], axes: List[Index]):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
def _from_blocks(cls, blocks: List[Block], axes: List[Index]):
def _from_blocks(cls: Type[T], blocks: List[Block], axes: List[Index]) -> T:

if not isinstance(block, Block):
block = make_block(block, placement=slice(0, len(axis)), ndim=1)
@classmethod
def _from_blocks(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

make this public as everything else is

@jreback jreback added this to the 1.1 milestone Mar 11, 2020
@jreback jreback merged commit b8ee1e1 into pandas-dev:master Mar 11, 2020
@jreback
Copy link
Contributor

jreback commented Mar 11, 2020

thanks

@jbrockmendel jbrockmendel deleted the block-init branch March 11, 2020 02:39
@@ -1500,6 +1511,8 @@ def __init__(
do_integrity_check: bool = False,
fastpath: bool = False,
):
assert isinstance(block, Block), type(block)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@simonjayhawkins is mypy reliable enough that we can remove this assertion? these add up in things like groupby.apply when we create many many Series objects

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is mypy reliable enough that we can remove this assertion?

not really as we don't have very strict type checking.

we could call the constructor with a variable that has type Any, or we could call the constructor from a function that is not currently type checked (still don't have check_untyped_defs for many modules)

to prevent calling the constructor with a variable with type Any, we would need disallow dynamic typing completely, https://mypy.readthedocs.io/en/stable/config_file.html#disallow-dynamic-typing

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Internals Related to non-user accessible pandas implementation Typing type annotations, mypy/pyright type checking
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants