Skip to content
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

Update type hints to python3 style (non-array files) #25802

Merged
merged 16 commits into from
Mar 30, 2019

Conversation

gwrome
Copy link
Contributor

@gwrome gwrome commented Mar 20, 2019

@gwrome
Copy link
Contributor Author

gwrome commented Mar 20, 2019

I'm unsure about the method I used to define TypeVars, so an instance method could refer to its own class type, e.g., in 014c48e

Is there a more appropriate way or a better place for the definition?

@WillAyd
Copy link
Member

WillAyd commented Mar 20, 2019

@gwrome thanks! I don't think we need any TypeVars just yet - can you not just implicitly break lines within brackets for container types as required?

@WillAyd WillAyd added the Typing type annotations, mypy/pyright type checking label Mar 20, 2019
@codecov
Copy link

codecov bot commented Mar 20, 2019

Codecov Report

Merging #25802 into master will increase coverage by <.01%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master   #25802      +/-   ##
==========================================
+ Coverage   91.27%   91.27%   +<.01%     
==========================================
  Files         173      173              
  Lines       53002    53014      +12     
==========================================
+ Hits        48375    48387      +12     
  Misses       4627     4627
Flag Coverage Δ
#multiple 89.84% <100%> (ø) ⬆️
#single 41.78% <84%> (+0.01%) ⬆️
Impacted Files Coverage Δ
pandas/core/dtypes/base.py 100% <100%> (ø) ⬆️
pandas/core/internals/blocks.py 94.09% <100%> (ø) ⬆️
pandas/core/indexes/period.py 92.06% <100%> (ø) ⬆️
pandas/core/indexes/datetimelike.py 98.52% <100%> (ø) ⬆️
pandas/core/base.py 97.77% <100%> (ø) ⬆️
pandas/core/groupby/groupby.py 97.21% <100%> (ø) ⬆️
pandas/core/internals/managers.py 93.93% <100%> (ø) ⬆️
pandas/core/frame.py 96.8% <100%> (ø) ⬆️
pandas/core/common.py 98.39% <100%> (ø) ⬆️
pandas/core/indexes/base.py 96.57% <100%> (ø) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 85c3f82...0efb21d. Read the comment docs.

@codecov
Copy link

codecov bot commented Mar 20, 2019

Codecov Report

Merging #25802 into master will decrease coverage by <.01%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master   #25802      +/-   ##
==========================================
- Coverage   91.48%   91.47%   -0.01%     
==========================================
  Files         175      175              
  Lines       52885    52872      -13     
==========================================
- Hits        48380    48366      -14     
- Misses       4505     4506       +1
Flag Coverage Δ
#multiple 90.04% <100%> (-0.01%) ⬇️
#single 41.81% <77.77%> (-0.02%) ⬇️
Impacted Files Coverage Δ
pandas/core/dtypes/base.py 100% <100%> (ø) ⬆️
pandas/core/internals/managers.py 93.93% <100%> (ø) ⬆️
pandas/core/indexes/datetimelike.py 98.53% <100%> (ø) ⬆️
pandas/core/base.py 97.77% <100%> (ø) ⬆️
pandas/core/groupby/groupby.py 97.21% <100%> (ø) ⬆️
pandas/core/frame.py 96.79% <100%> (-0.01%) ⬇️
pandas/core/common.py 98.39% <100%> (ø) ⬆️
pandas/core/indexes/base.py 96.57% <100%> (ø) ⬆️
pandas/plotting/_compat.py 86.66% <0%> (-0.84%) ⬇️
pandas/io/formats/terminal.py 31.7% <0%> (-0.83%) ⬇️
... and 22 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update d404460...ce92ce8. Read the comment docs.

gwrome added a commit to gwrome/pandas that referenced this pull request Mar 20, 2019
@gwrome
Copy link
Contributor Author

gwrome commented Mar 20, 2019

@WillAyd TIL about forward references in type hints, so that fixed what I was trying to address with TypeVars. They're all gone now.

pandas/core/groupby/groupby.py Outdated Show resolved Hide resolved
@@ -3632,8 +3632,7 @@ def values(self):
return self._data.view(np.ndarray)

@property
def _values(self):
# type: () -> Union[ExtensionArray, Index, np.ndarray]
def _values(self) -> Union[ExtensionArray, 'Index', np.ndarray]:
Copy link
Member

Choose a reason for hiding this comment

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

Is Index not a valid type here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Because it's used inside the Index class itself, Index isn't defined yet when types are checked, so we need to use a forward reference literal. See PEP 484. In that context, Index alone is an unresolved reference.

pandas/core/internals/blocks.py Outdated Show resolved Hide resolved
pandas/core/internals/managers.py Outdated Show resolved Hide resolved
@gwrome
Copy link
Contributor Author

gwrome commented Mar 20, 2019

Any guess as to why the MacOS 3.5 build fails? Did something change with respect to type hints between 3.5 and 3.6?

@WillAyd
Copy link
Member

WillAyd commented Mar 21, 2019

Can you run the test suite locally? The builds are pointing to a syntax error

@WillAyd
Copy link
Member

WillAyd commented Mar 21, 2019

Ah never mind I think the problem is that type annotations for class and instance variables came along later as part of PEP 526 in Python36, so for those probably best to stick with comment until we drop Py35 support

@gwrome
Copy link
Contributor Author

gwrome commented Mar 21, 2019

I returned the two variables class/instance variables to the previous style and left the rest of the changes intact.

Copy link
Member

@WillAyd WillAyd left a comment

Choose a reason for hiding this comment

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

This is really shaping up so good work! Just a few more things

pandas/core/frame.py Outdated Show resolved Hide resolved
pandas/core/dtypes/base.py Outdated Show resolved Hide resolved
pandas/core/frame.py Outdated Show resolved Hide resolved
@jreback
Copy link
Contributor

jreback commented Mar 22, 2019

we need a minimal run of mypy to validate things. it can be in this PR or a pre-cursor.

@WillAyd
Copy link
Member

WillAyd commented Mar 22, 2019

@jreback do you want that set up as part of CI or just validate output locally? I think the former is going to take a while as a pre-cursor so I figured switch over syntax first then clean up types, though open to however you want to approach

@gwrome
Copy link
Contributor Author

gwrome commented Mar 22, 2019

we need a minimal run of mypy to validate things. it can be in this PR or a pre-cursor.

The bad news is that running mypy on the changed files throws a ton of errors. The less bad news is that it appears that most of the errors are outside the scope of the changes in this PR.

I have some idea of how to proceed with the issues created by these type hints. The broader problems, though, will probably take substantial work outside the scope of this PR.

All the commands below were run using these numpy stubs.

pandas/core/base.py

I think these type hints are fine, but there are other errors, including an issue related to numpy, a missing pandas stub (which I presume hasn't been written yet), and some attribute errors in this file.

$ MYPYPATH=stubs/ mypy --follow-imports skip ./pandas/core/base.py
stubs/numpy/__init__.pyi:48: error: Class numpy.flatiter has abstract attributes "__next__"
stubs/numpy/__init__.pyi:48: note: If it is meant to be abstract, add 'abc.ABCMeta' as an explicit metaclass
pandas/core/base.py:10: error: No library stub file for module 'pandas._libs.lib'
pandas/core/base.py:10: note: (Stub files are from https://github.com/python/typeshed)
pandas/core/base.py:182: error: Module has no attribute "sum"
pandas/core/base.py:183: error: Module has no attribute "max"
pandas/core/base.py:184: error: Module has no attribute "min"
pandas/core/base.py:191: error: Module has no attribute "all"
pandas/core/base.py:192: error: Module has no attribute "any"
pandas/core/base.py:193: error: Module has no attribute "sum"
pandas/core/base.py:194: error: Module has no attribute "nansum"
pandas/core/base.py:195: error: Module has no attribute "mean"
pandas/core/base.py:196: error: Module has no attribute "nanmean"
pandas/core/base.py:197: error: Module has no attribute "prod"
pandas/core/base.py:198: error: Module has no attribute "nanprod"
pandas/core/base.py:199: error: Module has no attribute "std"
pandas/core/base.py:200: error: Module has no attribute "nanstd"
pandas/core/base.py:201: error: Module has no attribute "var"
pandas/core/base.py:202: error: Module has no attribute "nanvar"
pandas/core/base.py:203: error: Module has no attribute "median"
pandas/core/base.py:204: error: Module has no attribute "nanmedian"
pandas/core/base.py:205: error: Module has no attribute "max"
pandas/core/base.py:206: error: Module has no attribute "nanmax"
pandas/core/base.py:207: error: Module has no attribute "min"
pandas/core/base.py:208: error: Module has no attribute "nanmin"
pandas/core/base.py:209: error: Module has no attribute "cumprod"
pandas/core/base.py:210: error: Module has no attribute "nancumprod"
pandas/core/base.py:211: error: Module has no attribute "cumsum"
pandas/core/base.py:212: error: Module has no attribute "nancumsum"
pandas/core/base.py:852: error: "IndexOpsMixin" has no attribute "_values"; maybe "_map_values"?
pandas/core/base.py:975: error: "IndexOpsMixin" has no attribute "values"

pandas/core/common.py

The hints seem fine, but there are other errors, including an issue related to numpy, and a missing attribute error.

$ MYPYPATH=stubs/ mypy --follow-imports skip ./pandas/core/common.py
stubs/numpy/__init__.pyi:48: error: Class numpy.flatiter has abstract attributes "__next__"
stubs/numpy/__init__.pyi:48: note: If it is meant to be abstract, add 'abc.ABCMeta' as an explicit metaclass
pandas/core/common.py:133: error: Module has no attribute "any"

The remaining errors are similar to the other files.

pandas/core/dtypes/base.py

This one has a hint-related error:

pandas/core/dtypes/base.py:212: error: Invalid type "pandas.core.dtypes.base.ExtensionDtype.type"

Importing Type from typing and replacing type with Type corrects the error.

It also has similar numpy and missing attribute errors.

$ MYPYPATH=stubs/ mypy --follow-imports skip ./pandas/core/dtypes/base.py
stubs/numpy/__init__.pyi:48: error: Class numpy.flatiter has abstract attributes "__next__"
stubs/numpy/__init__.pyi:48: note: If it is meant to be abstract, add 'abc.ABCMeta' as an explicit metaclass
pandas/core/dtypes/base.py:28: error: Module has no attribute "nan"
pandas/core/dtypes/base.py:212: error: Invalid type "pandas.core.dtypes.base.ExtensionDtype.type"

pandas/core/frame.py

Miscellaneous errors unrelated to these hints.

$ MYPYPATH=stubs/ mypy --follow-imports skip ./pandas/core/frame.py
stubs/numpy/__init__.pyi:48: error: Class numpy.flatiter has abstract attributes "__next__"
stubs/numpy/__init__.pyi:48: note: If it is meant to be abstract, add 'abc.ABCMeta' as an explicit metaclass
pandas/core/frame.py:25: error: No library stub file for module 'numpy.ma'
pandas/core/frame.py:25: note: (Stub files are from https://github.com/python/typeshed)
pandas/core/frame.py:27: error: Module 'pandas._libs' has no attribute 'lib'
pandas/core/frame.py:27: error: Module 'pandas._libs' has no attribute 'algos'
pandas/core/frame.py:370: error: Need type annotation for '_accessors'
pandas/core/frame.py:397: error: No library stub file for module 'numpy.ma.mrecords'
pandas/core/frame.py:3790: error: Module has no attribute "nan"
pandas/core/frame.py:4716: error: No library stub file for module 'pandas._libs.hashtable'

pandas/core/groupby/groupby.py

This one has a few relevant errors:

pandas/core/groupby/groupby.py:328: error: Need type annotation for '_apply_whitelist'
pandas/core/groupby/groupby.py:1047: error: Incompatible types in assignment (expression has type "_ArrayLike[Any]", variable has type "ndarray[Any]")
pandas/core/groupby/groupby.py:1049: error: Incompatible return value type (got "Tuple[_ArrayLike[Any], Any]", expected "Tuple[ndarray[Any], Type[Any]]")
pandas/core/groupby/groupby.py:1052: error: Incompatible return value type (got "_ArrayLike[Any]", expected "ndarray[Any]")
pandas/core/groupby/groupby.py:1749: error: Incompatible types in assignment (expression has type "str", variable has type "Optional[Type[int64]]")
pandas/core/groupby/groupby.py:1750: error: Incompatible types in assignment (expression has type "_ArrayLike[Any]", variable has type "ndarray[Any]")
pandas/core/groupby/groupby.py:1762: error: Incompatible types in assignment (expression has type "_ArrayLike[Any]", variable has type "ndarray[Any]")

Almost all are a mismatch between _ArrayLike and ndarray. I guess those can be fixed by swapping the hints to expecting np._ArrayLike[Any]. Will need to make sure that's an accurate expectation.

Line 1749's error can be corrected by adding str to the second element of the return type, since the code can return np.int64, 'datetime64[ns]', or None.

$ MYPYPATH=stubs/ mypy --follow-imports skip ./pandas/core/groupby/groupby.py
stubs/numpy/__init__.pyi:48: error: Class numpy.flatiter has abstract attributes "__next__"
stubs/numpy/__init__.pyi:48: note: If it is meant to be abstract, add 'abc.ABCMeta' as an explicit metaclass
pandas/core/groupby/groupby.py:328: error: Need type annotation for '_apply_whitelist'
pandas/core/groupby/groupby.py:1047: error: Incompatible types in assignment (expression has type "_ArrayLike[Any]", variable has type "ndarray[Any]")
pandas/core/groupby/groupby.py:1047: error: Module has no attribute "bool"
pandas/core/groupby/groupby.py:1049: error: Incompatible return value type (got "Tuple[_ArrayLike[Any], Any]", expected "Tuple[ndarray[Any], Type[Any]]")
pandas/core/groupby/groupby.py:1049: error: Module has no attribute "bool"
pandas/core/groupby/groupby.py:1052: error: Incompatible return value type (got "_ArrayLike[Any]", expected "ndarray[Any]")
pandas/core/groupby/groupby.py:1749: error: Incompatible types in assignment (expression has type "str", variable has type "Optional[Type[int64]]")
pandas/core/groupby/groupby.py:1750: error: Incompatible types in assignment (expression has type "_ArrayLike[Any]", variable has type "ndarray[Any]")
pandas/core/groupby/groupby.py:1750: error: Module has no attribute "float"
pandas/core/groupby/groupby.py:1762: error: Incompatible types in assignment (expression has type "_ArrayLike[Any]", variable has type "ndarray[Any]")

pandas/core/indexes/base.py

Miscellaneous errors unrelated to these hints.

$ MYPYPATH=stubs/ mypy --follow-imports skip ./pandas/core/indexes/base.py
stubs/numpy/__init__.pyi:48: error: Class numpy.flatiter has abstract attributes "__next__"
stubs/numpy/__init__.pyi:48: note: If it is meant to be abstract, add 'abc.ABCMeta' as an explicit metaclass
pandas/core/indexes/base.py:9: error: Module 'pandas._libs' has no attribute 'algos'
pandas/core/indexes/base.py:9: error: Module 'pandas._libs' has no attribute 'index'
pandas/core/indexes/base.py:9: error: Module 'pandas._libs' has no attribute 'join'
pandas/core/indexes/base.py:11: error: No library stub file for module 'pandas._libs.lib'
pandas/core/indexes/base.py:11: note: (Stub files are from https://github.com/python/typeshed)
pandas/core/indexes/base.py:12: error: Module 'pandas._libs.tslibs' has no attribute 'OutOfBoundsDatetime'
pandas/core/indexes/base.py:12: error: Module 'pandas._libs.tslibs' has no attribute 'Timedelta'
pandas/core/indexes/base.py:12: error: Module 'pandas._libs.tslibs' has no attribute 'Timestamp'
pandas/core/indexes/base.py:13: error: No library stub file for module 'pandas._libs.tslibs.timezones'
pandas/core/indexes/base.py:150: error: Too many arguments for "dtype"
pandas/core/indexes/base.py:1784: error: Module has no attribute "nan"

pandas/core/indexes/datetimelike.py

Miscellaneous errors unrelated to these hints.

``` $ MYPYPATH=stubs/ mypy --follow-imports skip ./pandas/core/indexes/datetimelike.py stubs/numpy/__init__.pyi:48: error: Class numpy.flatiter has abstract attributes "__next__" stubs/numpy/__init__.pyi:48: note: If it is meant to be abstract, add 'abc.ABCMeta' as an explicit metaclass pandas/core/indexes/datetimelike.py:136: error: Decorated property not supported pandas/core/indexes/datetimelike.py:206: error: Name '_box_values' already defined on line 71 pandas/core/indexes/datetimelike.py:701: error: Need type annotation for '_raw_methods' pandas/core/indexes/datetimelike.py:703: error: Need type annotation for '_raw_properties' ```

pandas/core/internals/blocks.py

Miscellaneous errors unrelated to these hints.

$ MYPYPATH=stubs/ mypy --follow-imports skip ./pandas/core/internals/blocks.py
stubs/numpy/__init__.pyi:48: error: Class numpy.flatiter has abstract attributes "__next__"
stubs/numpy/__init__.pyi:48: note: If it is meant to be abstract, add 'abc.ABCMeta' as an explicit metaclass
pandas/core/internals/blocks.py:77: error: Module has no attribute "concatenate"
pandas/core/internals/blocks.py:2217: error: Definition of "_can_hold_na" in base class "ExtensionBlock" is incompatible with definition in base class "DatetimeBlock"

pandas/core/internals/managers.py

Numpy errors.

$ MYPYPATH=stubs/ mypy --follow-imports skip ./pandas/core/internals/managers.py
stubs/numpy/__init__.pyi:48: error: Class numpy.flatiter has abstract attributes "__next__"
stubs/numpy/__init__.pyi:48: note: If it is meant to be abstract, add 'abc.ABCMeta' as an explicit metaclass

@jreback
Copy link
Contributor

jreback commented Mar 22, 2019

@jreback do you want that set up as part of CI or just validate output locally? I think the former is going to take a while as a pre-cursor so I figured switch over syntax first then clean up types, though open to however you want to approach

we absolutely need CI for this, which excludes non-fixed files.

pandas/core/groupby/groupby.py Outdated Show resolved Hide resolved
@WillAyd
Copy link
Member

WillAyd commented Mar 26, 2019

@gwrome can you merge master?

@gwrome
Copy link
Contributor Author

gwrome commented Mar 26, 2019

You got it, @WillAyd

pandas/core/dtypes/base.py Outdated Show resolved Hide resolved
Copy link
Member

@WillAyd WillAyd left a comment

Choose a reason for hiding this comment

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

lgtm @jreback

@WillAyd WillAyd added this to the 0.25.0 milestone Mar 28, 2019
gwrome added a commit to gwrome/pandas that referenced this pull request Mar 28, 2019
@jreback jreback merged commit 2792705 into pandas-dev:master Mar 30, 2019
@jreback
Copy link
Contributor

jreback commented Mar 30, 2019

thanks @gwrome

@gwrome gwrome deleted the update-type-hints-python3 branch March 31, 2019 14:46
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.

3 participants