Skip to content

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

Closed
wants to merge 1 commit into from

Conversation

MarcoGorelli
Copy link
Member

  • Ensure all linting tests pass, see here for how to run them

@MarcoGorelli MarcoGorelli added Code Style Code style, linting, code_checks Typing type annotations, mypy/pyright type checking labels Jan 19, 2021
language: pygrep
types: [python]
exclude: ^pandas/_typing\.py$
Copy link
Member

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 =

Copy link
Member

@simonjayhawkins simonjayhawkins Jan 19, 2021

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.

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'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)

Copy link
Member

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]
Copy link
Member

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.

Copy link
Member Author

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

Copy link
Member

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

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.

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.

@simonjayhawkins simonjayhawkins added this to the 1.3 milestone Jan 19, 2021
@MarcoGorelli
Copy link
Member Author

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

@MarcoGorelli
Copy link
Member Author

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 🙏

@MarcoGorelli MarcoGorelli deleted the generic-strings branch March 9, 2021 14:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Code Style Code style, linting, code_checks Typing type annotations, mypy/pyright type checking
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants