-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
CLN, TYP use string types in generics #39270
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
Conversation
MarcoGorelli
commented
Jan 19, 2021
- Ensure all linting tests pass, see here for how to run them
language: pygrep | ||
types: [python] | ||
exclude: ^pandas/_typing\.py$ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is this because of aliases? If so, we may also define aliases local to a module, so we would need to allow if preceeded by =
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we also have the issue of casts and TypeVars, which still require the quoting.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's not because of aliases (they're fine), but because in _typing
we would otherwise get
ImportError while loading conftest '/home/marco/pandas-marco/pandas/conftest.py'.
pandas/__init__.py:22: in <module>
from pandas.compat.numpy import (
pandas/compat/__init__.py:14: in <module>
from pandas._typing import F
pandas/_typing.py:79: in <module>
FrameOrSeriesUnion = Union["DataFrame", Series]
E NameError: name 'Series' is not defined
because Series
is only defined if TYPE_CHECKING
(same situation as in the other comment)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yep, I wasn't clear. it's not just type aliases https://www.python.org/dev/peps/pep-0563/#forward-references
This PEP addresses the issue of forward references in type annotations. The use of string literals will no longer be required in this case. However, there are APIs in the typing module that use other syntactic constructs of the language, and those will still require working around forward references with string literals. The list includes:
ArrayConvertible = Union[List, Tuple, AnyArrayLike, Series] | ||
Scalar = Union[int, float, str] | ||
DatetimeScalar = TypeVar("DatetimeScalar", Scalar, datetime) | ||
DatetimeScalarOrArrayConvertible = Union[DatetimeScalar, ArrayConvertible] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess this is also an option. would prefer to update regex though.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure - this wasn't for the regex though, but rather because otherwise we get
ImportError while loading conftest '/home/marco/pandas-marco/pandas/conftest.py'.
pandas/__init__.py:51: in <module>
from pandas.core.api import (
pandas/core/api.py:31: in <module>
from pandas.core.groupby import Grouper, NamedAgg
pandas/core/groupby/__init__.py:1: in <module>
from pandas.core.groupby.generic import DataFrameGroupBy, NamedAgg, SeriesGroupBy
pandas/core/groupby/generic.py:69: in <module>
from pandas.core.frame import DataFrame
pandas/core/frame.py:156: in <module>
from pandas.core.series import Series
pandas/core/series.py:108: in <module>
from pandas.core.tools.datetimes import to_datetime
pandas/core/tools/datetimes.py:74: in <module>
ArrayConvertible = Union[List, Tuple, AnyArrayLike, Series]
E NameError: name 'Series' is not defined
because here Series
is only defined if TYPE_CHECKING
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://www.python.org/dev/peps/pep-0563/#forward-references goes on to say
Depending on the specific case, some of the cases listed above might be worked around by placing the usage in a if TYPE_CHECKING: block. This will not work for any code that needs to be available at runtime, notably for base classes and casting. For named tuples, using the new class definition syntax introduced in Python 3.6 solves the issue.
so this is fine
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @MarcoGorelli lgtm except for the chances of false positives. We probably need to use a AST parser to do this checking in the longer term.
Agreed - it's in my backlog to make sense of that and to make a better solution (which could be its own package), will get back to you on it when I make progress |
I just gave it a go and I think I have something which does the job! Will make a PR tomorrow, we can close this one, thanks for pushing me to do it properly 🙏 |