-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
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
Conversation
pandas/core/internals/managers.py
Outdated
@@ -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) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
blocks -> arr
pandas/core/internals/managers.py
Outdated
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 |
There was a problem hiding this comment.
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
pandas/core/internals/managers.py
Outdated
@@ -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: |
There was a problem hiding this comment.
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
sorry @jbrockmendel didn't get round to this today. will look tomorrow. |
pandas/core/internals/managers.py
Outdated
@@ -149,6 +151,13 @@ def __init__( | |||
self._blknos = None | |||
self._blklocs = None | |||
|
|||
@classmethod | |||
def _from_blocks(cls, blocks: List[Block], axes: List[Index]): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
def _from_blocks(cls, blocks: List[Block], axes: List[Index]): | |
def _from_blocks(cls: Type[T], blocks: List[Block], axes: List[Index]) -> T: |
pandas/core/internals/managers.py
Outdated
if not isinstance(block, Block): | ||
block = make_block(block, placement=slice(0, len(axis)), ndim=1) | ||
@classmethod | ||
def _from_blocks( |
There was a problem hiding this comment.
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
thanks |
@@ -1500,6 +1511,8 @@ def __init__( | |||
do_integrity_check: bool = False, | |||
fastpath: bool = False, | |||
): | |||
assert isinstance(block, Block), type(block) |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
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.