Skip to content

CLN: Assorted typings #28604

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 3 commits into from
Oct 1, 2019
Merged

CLN: Assorted typings #28604

merged 3 commits into from
Oct 1, 2019

Conversation

jbrockmendel
Copy link
Member

Broken off from abandoned local branches.

Also fixes one of the problems currently afflicting the CI in tests.groupby.test_categorical

@simonjayhawkins simonjayhawkins added the Typing type annotations, mypy/pyright type checking label Sep 25, 2019
index: bool = True,
encoding: str = "utf8",
hash_key=None,
categorize: bool = True,
Copy link
Member

Choose a reason for hiding this comment

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

adding type hints here is causing pandas/core/util/hashing.py:132: error: Incompatible types in assignment (expression has type "chain[Any]", variable has type "Generator[Any, None, None]") further down as the body is now checked.

Copy link
Member

Choose a reason for hiding this comment

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

it looks like the conda environment now has mypy 0.720. result!

fix is here 304351e is you want to include in the PR. (can't use py3.6 variable annotations though.)

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.

@jbrockmendel i was a bit hasty in my approval. a couple of new mypy errors arising from adding additional type hints

pandas/core/algorithms.py:1938: error: Incompatible types in assignment (expression has type "Tuple[slice, ...]", variable has type "List[slice]")
pandas/core/algorithms.py:1942: error: Incompatible types in assignment (expression has type "Tuple[slice, ...]", variable has type "List[slice]")

@jreback jreback added this to the 1.0 milestone Sep 25, 2019

# keep `hashes` specifically a generator to keep mypy happy
_hashes = itertools.chain(hashes, index_hash_generator)
hashes = (x for x in _hashes)
Copy link
Contributor

Choose a reason for hiding this comment

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

you can also do a cast here (and in the change above), not sure what is more idiomatic & what we have settled on

cc @simonjayhawkins

Copy link
Member

Choose a reason for hiding this comment

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

you can also do a cast here

Our type hints policy is "..., the use of cast is strongly discouraged. Where applicable a refactor of the code to appease static analysis is preferable". https://dev.pandas.io/docs/development/contributing.html#style-guidelines

personally I'm not a fan of changing the runtime behavior of code just to appease mypy if not absolutely necessary unless it makes for cleaner code. (This applies to both our policy and the changes in this PR)

The problem in this function is because Mypy considers the initial assignment as the definition of a variable. https://mypy.readthedocs.io/en/stable/type_inference_and_annotations.html#type-inference which occurs on L114.
mypy infers the type as Generator[Any, None, None].

it is possible to override the inferred type of a variable by using a variable type annotation https://mypy.readthedocs.io/en/stable/type_inference_and_annotations.html#explicit-types-for-variables

This is the approach I took to silence this mypy error in 304351e. I prefer this as it has no effect on the runtime behavior.

with regard to the use of a leading underscore to avoid Incompatible types in assignment.. errors, my preference would be to use a trailing underscore. In PEP8, a trailing underscore is used by convention to avoid conflicts with Python keywords. So I see more similarity here to avoid conflicts than the leading underscore idiom. Not sure what's best here, but just my POV if we want to choose a pattern to be consistent moving forward.

Copy link
Member Author

Choose a reason for hiding this comment

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

It looks like the main actionable suggestion here is to change _hashes to hashes_; is that right @simonjayhawkins ? I'm fine with that.

@WillAyd WillAyd merged commit 4d79b53 into pandas-dev:master Oct 1, 2019
@WillAyd
Copy link
Member

WillAyd commented Oct 1, 2019

Thanks @jbrockmendel

josibake pushed a commit to josibake/pandas that referenced this pull request Oct 1, 2019
@jbrockmendel jbrockmendel deleted the cln branch October 1, 2019 13:33
proost pushed a commit to proost/pandas that referenced this pull request Dec 19, 2019
proost pushed a commit to proost/pandas that referenced this pull request Dec 19, 2019
bongolegend pushed a commit to bongolegend/pandas that referenced this pull request Jan 1, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Typing type annotations, mypy/pyright type checking
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants