Skip to content

TYP: require Index objects earlier in internals #33100

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 6 commits into from
Apr 4, 2020

Conversation

jbrockmendel
Copy link
Member

No description provided.

def create_block_manager_from_arrays(
arrays, names: Index, axes: List[Index]
) -> BlockManager:
assert isinstance(names, Index)
Copy link
Member

Choose a reason for hiding this comment

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

In theory mypy will flag violations here already for us, so superfluous to have these here

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 this correct? in a previous pass I found there were annotation-defying types being passed in this function that mypy had not caught

Copy link
Member

Choose a reason for hiding this comment

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

yes, @WillAyd is correct, but in practice we probably need the run-time asserts to be sure. probably better to fail than potentially produce incorrect results.

in a previous pass I found there were annotation-defying types being passed in this function that mypy had not caught

if a variable is Any or the callsite is not being checked, then we can pass the wrong type.

if passed == implied and e is not None:
raise e
return e
Copy link
Member

Choose a reason for hiding this comment

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

Rather than do this I think would be better to just mark this function with typing.NoReturn as the return value

Copy link
Member

Choose a reason for hiding this comment

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

i recall that fails on our min version build.

Copy link
Member Author

Choose a reason for hiding this comment

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

elsewhere (in particular in indexes.base) we've moved towards this pattern

# put "leftover" items in float bucket, where else?
# generalize?
items_dict = defaultdict(list)
items_dict: Dict[str, List] = defaultdict(list)
Copy link
Member

Choose a reason for hiding this comment

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

Was this throwing an error? I don't think this is required but if so should type using typing.DefaultDict

@@ -1738,7 +1745,7 @@ def form_blocks(arrays, names, axes):
block_type = get_block_type(v)
items_dict[block_type.__name__].append((i, k, v))

blocks = []
blocks: List[Block] = []
Copy link
Member

Choose a reason for hiding this comment

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

Just as FYI but newer versions of mypy technically have better inferencing capabilities for empty sequence assignment like this. @simonjayhawkins

Copy link
Member Author

Choose a reason for hiding this comment

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

id be happy to remove this; is that the suggested course of action?

@WillAyd WillAyd added the Clean label Mar 30, 2020
Copy link
Member

@simonjayhawkins simonjayhawkins left a comment

Choose a reason for hiding this comment

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

Thanks @jbrockmendel generally lgtm ex @WillAyd comments. A couple of suggestions but not blockers. The asserts are primarily for runtime debugging and not mypy?

@@ -74,6 +77,8 @@ def arrays_to_mgr(arrays, arr_names, index, columns, dtype=None, verify_integrit
# from BlockManager perspective
axes = [columns, index]

assert isinstance(columns, Index), type(columns)
assert isinstance(index, Index), type(index)
Copy link
Member

Choose a reason for hiding this comment

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

ensure_index on L70 and L75 should return an Index. can you type ensure_index instead of adding asserts

@@ -209,6 +215,8 @@ def init_ndarray(values, index, columns, dtype=None, copy=False):
else:
block_values = [values]

assert isinstance(columns, Index), type(columns)
assert isinstance(index, Index), type(index)
Copy link
Member

Choose a reason for hiding this comment

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

similar comment as above, can we type _get_axes instead of adding asserts.

if passed == implied and e is not None:
raise e
return e
Copy link
Member

Choose a reason for hiding this comment

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

i recall that fails on our min version build.

@simonjayhawkins simonjayhawkins added this to the 1.1 milestone Mar 30, 2020
@simonjayhawkins simonjayhawkins added the Typing type annotations, mypy/pyright type checking label Mar 30, 2020
@jbrockmendel
Copy link
Member Author

i think requests have been addressed

Copy link
Member

@simonjayhawkins simonjayhawkins left a comment

Choose a reason for hiding this comment

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

Copy link
Contributor

@jreback jreback left a comment

Choose a reason for hiding this comment

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

@WillAyd WillAyd merged commit 5e21be0 into pandas-dev:master Apr 4, 2020
@WillAyd
Copy link
Member

WillAyd commented Apr 4, 2020

Thanks @jbrockmendel

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Clean Typing type annotations, mypy/pyright type checking
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants