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

TYP: pandas/core/dtypes/dtypes.py #31384

Conversation

simonjayhawkins
Copy link
Member

No description provided.

@simonjayhawkins simonjayhawkins added the Typing type annotations, mypy/pyright type checking label Jan 28, 2020
str_type = str

ExtensionDtypeT = TypeVar("ExtensionDtypeT", bound=ExtensionDtype)
Copy link
Member

Choose a reason for hiding this comment

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

When do you need to use ExtensionDtypeT and when can you use ExtensionDtype (both are used in this file/diff)

Copy link
Member Author

Choose a reason for hiding this comment

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

not using a typevar for find may be an oversight, I'll look into whether necessary.

Copy link
Member Author

Choose a reason for hiding this comment

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

the only callsites are in pandas\core\dtypes\common.py, so find may need typevars once types are added there, and the callsites to there are typed etc. etc.

There does seem to be some resistance to typevars, shall I add now or wait till becomes necessity?

I've also just noticed that the type annotation for dtype in find is also incorrect...

the code is

            if not isinstance(dtype, type):
                dtype_type = type(dtype)
            if issubclass(dtype_type, ExtensionDtype):
                return dtype

so dtype parameter can also accept an instance as well as a type. again could update here or in a subsequent PR. only the return type was changed in this PR as this caused errors once construct_from_string was typed as this returns an instance, return annotation on master is only
type.

Copy link
Member Author

Choose a reason for hiding this comment

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

The typevar has been removed for now.

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.

question but generally looks good. need to merge master

raise ValueError(msg)
else:
raise ValueError("DatetimeTZDtype only supports ns units")
self._unit, self._tz = unit.unit, unit.tz
Copy link
Member

Choose a reason for hiding this comment

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

Just out of curiosity why did this need to change?

Copy link
Member Author

Choose a reason for hiding this comment

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

This gives pandas\core\dtypes\dtypes.py:684: error: "str" has no attribute "tz" which looks weird considering the isinstance check and maybe a mypy issue.

if we split the tuple assignment (which would obviously be wrong), may explain the message

        if isinstance(unit, DatetimeTZDtype):
            reveal_type(unit)
            reveal_type(unit.unit)
            reveal_type(unit.tz)
            unit = unit.unit
            tz = unit.tz
            reveal_type(unit)
pandas\core\dtypes\dtypes.py:683: note: Revealed type is 'pandas.core.dtypes.dtypes.DatetimeTZDtype'
pandas\core\dtypes\dtypes.py:684: note: Revealed type is 'builtins.str'
pandas\core\dtypes\dtypes.py:685: note: Revealed type is 'Any'
pandas\core\dtypes\dtypes.py:687: error: "str" has no attribute "tz"
pandas\core\dtypes\dtypes.py:688: note: Revealed type is 'builtins.str'

Copy link
Member

Choose a reason for hiding this comment

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

Yea that seems weird. If we just type ignore does anything else need to change? Slight preference for keeping as is and opening an issue on mypy for narrowing issue, if one doesn’t already exist

Copy link
Member Author

Choose a reason for hiding this comment

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

reversing the assignment does not give a false positive, so could do this instead..

          tz, unit = unit.tz, unit.unit

Copy link
Member Author

Choose a reason for hiding this comment

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

or we could ignore.

Copy link
Member

Choose a reason for hiding this comment

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

Reversing works as well but in any case would be helpful to raise with mypy

@jreback
Copy link
Contributor

jreback commented Feb 2, 2020

can you rebase

@@ -65,7 +85,7 @@ def register(self, dtype: Type[ExtensionDtype]) -> None:
"""
Parameters
----------
dtype : ExtensionDtype
dtype : Type[ExtensionDtype]
Copy link
Member

Choose a reason for hiding this comment

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

Can you put this in a non-typing way? Eg "ExtensionDtype class"

@jbrockmendel
Copy link
Member

this looks fine; any sticking points?

@WillAyd
Copy link
Member

WillAyd commented Mar 11, 2020

@simonjayhawkins can you merge master?

@jreback jreback added this to the 1.1 milestone Mar 11, 2020
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.

yeah merge master and ping on green.

@@ -13,6 +24,15 @@
from pandas.core.dtypes.generic import ABCCategoricalIndex, ABCDateOffset, ABCIndexClass
from pandas.core.dtypes.inference import is_bool, is_list_like

if TYPE_CHECKING:
import pyarrow # noqa: F401
Copy link
Member

Choose a reason for hiding this comment

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

This means that running mypy (eg in a pre-commit hook) requires pyarrow to be installed? (which is not a required dependency?)
Are we fine with that? (do we already do that for other deps?)

Copy link
Member Author

Choose a reason for hiding this comment

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

This means that running mypy (eg in a pre-commit hook) requires pyarrow to be installed?

no. mypy is a static checker.

The behavior if pyarrow is not installed depends on the strictness of the type checking.(ignore_missing_imports config option)

in general,"mypy will assume the type of that module is Any, the dynamic type. This means attempting to access any attribute of the module will automatically succeed"
https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports

@WillAyd
Copy link
Member

WillAyd commented Mar 27, 2020

I think this was lost in the shuffle @simonjayhawkins . If you can merge master to be safe just merge on green

@simonjayhawkins simonjayhawkins merged commit 8415f9b into pandas-dev:master Apr 5, 2020
jbrockmendel pushed a commit to jbrockmendel/pandas that referenced this pull request Apr 6, 2020
@simonjayhawkins simonjayhawkins deleted the pandas/core/dtypes/dtypes.py branch April 27, 2020 09:01
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.

5 participants