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

CLN: Fixing mypy errors in pandas/conftest.py #29046

Merged
merged 16 commits into from
Mar 25, 2020

Conversation

angelaambroz
Copy link
Contributor

@angelaambroz angelaambroz commented Oct 17, 2019

@angelaambroz angelaambroz changed the title Ignoring some mypy errors related to imports and list inference CLN: Fixing mypy errors in pandas/conftest.py Oct 17, 2019
@WillAyd
Copy link
Member

WillAyd commented Oct 17, 2019 via email

@angelaambroz
Copy link
Contributor Author

Cool - hadn't seen pandas._typing. Will do.

@pep8speaks
Copy link

pep8speaks commented Oct 17, 2019

Hello @angelaambroz! Thanks for updating this PR. We checked the lines you've touched for PEP 8 issues, and found:

There are currently no PEP 8 issues detected in this Pull Request. Cheers! 🍻

Comment last updated at 2020-03-25 18:36:23 UTC

@angelaambroz
Copy link
Contributor Author

I've left the # type: ignore for the import statement, since I suspect it's related to what the mypy docs describe when a module is present, but mypy is unable - for whatever reason - to check its type hints.

@@ -481,7 +482,7 @@ def tz_aware_fixture(request):

UNSIGNED_INT_DTYPES = ["uint8", "uint16", "uint32", "uint64"]
UNSIGNED_EA_INT_DTYPES = ["UInt8", "UInt16", "UInt32", "UInt64"]
SIGNED_INT_DTYPES = [int, "int8", "int16", "int32", "int64"]
SIGNED_INT_DTYPES = [int, "int8", "int16", "int32", "int64"] # type: Dtype
Copy link
Member

Choose a reason for hiding this comment

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

This is actually a List[Dtype]

Copy link
Member

@WillAyd WillAyd Oct 17, 2019

Choose a reason for hiding this comment

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

I think Dtype needs to be updated in pandas._typing to include Type[Union[float, int, str, bool]] for when the built-in values get provided - could you make that change as well?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yep, sure - thanks for the guidance.

Copy link
Member

Choose a reason for hiding this comment

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

Type[Union[float, str, bool]] would be sufficient as int is duck type compatible with float see https://mypy.readthedocs.io/en/latest/duck_type_compatibility.html#duck-type-compatibility

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks, both. Just pushed what I think you both meant - let me know. I'm still learning about types! Looks like complex was also duck typed to float.

@@ -8,9 +8,10 @@
from hypothesis import strategies as st
import numpy as np
import pytest
from pytz import FixedOffset, utc
from pytz import FixedOffset, utc # type: ignore
Copy link
Member

Choose a reason for hiding this comment

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

This is the same as #28932 - I think this needs to be fixed upstream in Typeshed. Would you be interested in submitting that there?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure - I can give it a shot.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Picking this up again - just got the changes merged in typeshed (python/typeshed#3393). I see that there are now some conflicts with master; I can address those and pick this up again in the next couple days.

Copy link
Member

Choose a reason for hiding this comment

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

Great job and thanks for fixing that upstream! Still not a rush on our end but should make it into the next mypy release. Will be helpful for us in a few places

@simonjayhawkins simonjayhawkins added the Typing type annotations, mypy/pyright type checking label Oct 17, 2019
@WillAyd
Copy link
Member

WillAyd commented Oct 19, 2019 via email

pandas/_typing.py Outdated Show resolved Hide resolved
@@ -18,7 +18,7 @@
AnyArrayLike = TypeVar("AnyArrayLike", "ExtensionArray", "Index", "Series", np.ndarray)
ArrayLike = TypeVar("ArrayLike", "ExtensionArray", np.ndarray)
DatetimeLikeScalar = TypeVar("DatetimeLikeScalar", "Period", "Timestamp", "Timedelta")
Dtype = Union[str, np.dtype, "ExtensionDtype"]
Dtype = Type[Union[str, float, bool, np.dtype, "ExtensionDtype"]]
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
Dtype = Type[Union[str, float, bool, np.dtype, "ExtensionDtype"]]
Dtype = Union[ExtensionDtype, Type[Union[str, float, int, complex, bool]]]

Somewhat more complicated but the ExtensionDtype needs to be an instance, so should exclude from the Type element.

To illustrate:

>>> pd.Series(range(1), dtype=pd.Int64Dtype)  # Not what you would expect
0    0
dtype: object
>>> pd.Series(range(1), dtype=pd.Int64Dtype())  # This instance works though
0    0
dtype: object
>>> pd.Series(range(1), dtype=int) # contrast to the builtin, which uses Type[int]
0    0
dtype: int64

cc @TomAugspurger in case there's something I'm missing with the ExtensionDtype

Also @simonjayhawkins do you mind if we include int in the Union? Understood that it gets replaced as part of the mypy type hierarchy, but given dtype=int is a valid construct it serves as literal documentation and requires less knowledge of how mypy works. Whenever we get to Python 3.8 as the min version these should be converted to Literal annotations anyway

Copy link
Contributor

Choose a reason for hiding this comment

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

>>> pd.Series(range(1), dtype=pd.Int64Dtype)

should probably raise. I don't think we can automatically make that work since we have parametrized dtypes. Not sure what that does to the type hint.

Copy link
Member

Choose a reason for hiding this comment

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

Also @simonjayhawkins do you mind if we include int in the Union?

no problem.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

👋 Sorry for the silence on my end. I'm picking this up again now. I didn't follow your comment, @WillAyd (though I committed your suggestion - hence the Outdated flag). Does this requested review still apply? Thanks.

@alimcmaster1
Copy link
Member

@angelaambroz thanks for the PR! Are you still working on this?

@angelaambroz
Copy link
Contributor Author

Eek, sorry, yes. I'd like to keep working on this. Apologies for the silence.

@@ -481,14 +483,14 @@ def tz_aware_fixture(request):

UNSIGNED_INT_DTYPES = ["uint8", "uint16", "uint32", "uint64"]
UNSIGNED_EA_INT_DTYPES = ["UInt8", "UInt16", "UInt32", "UInt64"]
SIGNED_INT_DTYPES = [int, "int8", "int16", "int32", "int64"]
SIGNED_INT_DTYPES = [int, "int8", "int16", "int32", "int64"] # type: List[Dtype]
Copy link
Member

Choose a reason for hiding this comment

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

will need to change to py3.6 syntax.

STRING_DTYPES = [str, "str", "U"]
FLOAT_DTYPES = [float, "float32", "float64"] # type: List[Dtype]
COMPLEX_DTYPES = [complex, "complex64", "complex128"] # type: List[Dtype]
STRING_DTYPES = [str, "str", "U"] # type: List[Dtype]
Copy link
Member

Choose a reason for hiding this comment

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

same

@angelaambroz
Copy link
Contributor Author

CI fails on pytest pandas/conftest.py, due to ExtensionDtype not being recognized. It's currently only imported if TYPE_CHECKING==True. There's a comment warning against circular imports, so I'm hesitating moving

from pandas.core.dtypes.dtypes import ExtensionDtype  # noqa: F401

out of the conditional. Halp? 🆘

pandas/_typing.py Outdated Show resolved Hide resolved
@datapythonista
Copy link
Member

@angelaambroz can you take care of the last comment, so we can finally merge this please? Please ping us once the CI is green, for a faster merge.

@angelaambroz
Copy link
Contributor Author

angelaambroz commented Dec 29, 2019 via email

@datapythonista
Copy link
Member

You can do the last commit from the website, there is a dropdown to accept the proposed change. I tried to do it myself, but looks like GitHub just let the author accept it.

Co-Authored-By: William Ayd <william.ayd@icloud.com>
@angelaambroz
Copy link
Contributor Author

angelaambroz commented Dec 29, 2019

Ack. CI failing linting - it doesn't like the import order in conftest.

  • I could try fixing this via the GitHub web UI (iffy, but I might be able to do it).
  • Someone else picks this up.
  • We wait until Jan 8/9, when I'm back at a computer and can debug properly.

@alimcmaster1
Copy link
Member

Ack. CI failing linting - it doesn't like the import order in conftest.

  • I could try fixing this via the GitHub web UI (iffy, but I might be able to do it).
  • Someone else picks this up.
  • We wait until Jan 8/9, when I'm back at a computer and can debug properly.

You can run isort -rc pandas to fix the import order issue. For more info see here: https://pandas.pydata.org/pandas-docs/stable/development/contributing.html#import-formatting

@WillAyd
Copy link
Member

WillAyd commented Jan 16, 2020

@angelaambroz can you fix up merge conflict? I think this is pretty much ready

@angelaambroz
Copy link
Contributor Author

It's failing on type checks now; this was unexpected, but I didn't dive deep into the merge conflicts on pandas/_typing.py, where I saw some changes (I intended just to change the Dtype type). I'll try to go through this later today and see what's up.

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.

I think just need to add str as a valid dtype to resolve CI errors. Something like dtype="boolean" is valid

@@ -44,7 +45,7 @@

# other

Dtype = Union[str, np.dtype, "ExtensionDtype"]
Dtype = Union["ExtensionDtype", Type[Union[str, float, int, complex, bool]]]
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
Dtype = Union["ExtensionDtype", Type[Union[str, float, int, complex, bool]]]
Dtype = Union["ExtensionDtype", str, Type[Union[str, float, int, complex, bool]]]

@simonjayhawkins
Copy link
Member

@angelaambroz can you merge master

@MarcoGorelli
Copy link
Member

Hi @angelaambroz - sorry to chase you up, just wanted to ask whether you're still working on this :)

@angelaambroz
Copy link
Contributor Author

@MarcoGorelli Feel free to grab it - I'm being very slow and not finding enough time to get back to this! I'd be happy to continue, but don't want to hold things up. Thanks for checking in!

@MarcoGorelli
Copy link
Member

I'd be happy to continue, but don't want to hold things up

It's OK, I don't believe this is too urgent, so feel free to continue and to ask for help if you're stuck

@WillAyd
Copy link
Member

WillAyd commented Mar 11, 2020

@angelaambroz any interest in fixing up the merge conflict and addressing last comment?

@angelaambroz
Copy link
Contributor Author

👋 Yes, here! I hang my head in shame. But I can do this later today.

@WillAyd WillAyd added this to the 1.1 milestone Mar 25, 2020
@WillAyd WillAyd merged commit 9616d98 into pandas-dev:master Mar 25, 2020
@WillAyd
Copy link
Member

WillAyd commented Mar 25, 2020

Thanks @angelaambroz for seeing this through

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.

8 participants