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

UP035 false positive on typing_extensions.dataclass_transform on python 3.11 #5112

Closed
DetachHead opened this issue Jun 15, 2023 · 7 comments · Fixed by #5291
Closed

UP035 false positive on typing_extensions.dataclass_transform on python 3.11 #5112

DetachHead opened this issue Jun 15, 2023 · 7 comments · Fixed by #5291
Labels
good first issue Good for newcomers help wanted Contributions especially welcome rule Implementing or modifying a lint rule

Comments

@DetachHead
Copy link

from typing_extensions import dataclass_transform
# UP035 Import from `typing` instead: `dataclass_transform`

typing_extensions.dataclass_transform has the frozen_default parameter which won't be in typing until python 3.12.

from typing_extensions:

if sys.version_info >= (3, 12):
    # dataclass_transform exists in 3.11 but lacks the frozen_default parameter
    dataclass_transform = typing.dataclass_transform
else:
    def dataclass_transform(
        *,
        eq_default: bool = True,
        order_default: bool = False,
        kw_only_default: bool = False,
        frozen_default: bool = False,
        field_specifiers: typing.Tuple[
            typing.Union[typing.Type[typing.Any], typing.Callable[..., typing.Any]],
            ...
        ] = (),
        **kwargs: typing.Any,
    ) -> typing.Callable[[T], T]:
        ...
@charliermarsh
Copy link
Member

Ah interesting, okay, thank you.

@charliermarsh charliermarsh added rule Implementing or modifying a lint rule help wanted Contributions especially welcome good first issue Good for newcomers labels Jun 15, 2023
@AlexWaygood
Copy link
Member

AlexWaygood commented Jun 16, 2023

Looks like ruff will also do this if you run ruff --fix --select="UP035":

- from typing_extensions import SupportsIndex
+ from typing import SupportsIndex

  isinstance(42, SupportsIndex)

But the isinstance() check there is 20x faster if you use the typing_extensions version, as typing_extensions backports a ton of optimisations that have been added to typing.Protocol in Python 3.12

@charliermarsh
Copy link
Member

@AlexWaygood - Would you suggest not rewriting Protocol imports either for the same reason?

@AlexWaygood
Copy link
Member

AlexWaygood commented Jun 22, 2023

@AlexWaygood - Would you suggest not rewriting Protocol imports either for the same reason?

Yes -- and FWIW, pyupgrade recently made that change in asottile/pyupgrade@5059713, which is included in pyupgrade v3.7 (following my PR asottile/reorder-python-imports#341 to the reorder-python-imports project, which pyupgrade uses).

@charliermarsh
Copy link
Member

Thanks!

@AlexWaygood
Copy link
Member

AlexWaygood commented Jun 22, 2023

following my PR asottile/reorder-python-imports#341 to the reorder-python-imports project, which pyupgrade uses

...which, looking at it again, it seems I forgot to include SupportsIndex in that PR, so I guess pyupgrade will continue to auto-replace from typing_extensions import SupportsIndex with from typing import SupportsIndex 🙃

Ruff can show its value-add here by not making that mistake :D

@charliermarsh
Copy link
Member

Hahah oh no 😂

charliermarsh pushed a commit that referenced this issue Jun 22, 2023
## Summary

Remove recommendations to replace
`typing_extensions.dataclass_transform` and
`typing_extensions.SupportsIndex` with their `typing` library
counterparts.

Closes #5112.

## Test Plan

Added extra checks to the test fixture.

`cargo test`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers help wanted Contributions especially welcome rule Implementing or modifying a lint rule
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants