mirror of
https://github.com/astral-sh/ruff.git
synced 2025-10-01 06:11:43 +00:00

## Summary This PR extends our [PEP 695](https://peps.python.org/pep-0695) handling from the type aliases handled by `UP040` to generic function and class parameters, as suggested in the latter two examples from #4617: ```python # Input T = TypeVar("T", bound=float) class A(Generic[T]): ... def f(t: T): ... # Output class A[T: float]: ... def f[T: float](t: T): ... ``` I first implemented this as part of `UP040`, but based on a brief discussion during a very helpful pairing session with @AlexWaygood, I opted to split them into rules separate from `UP040` and then also separate from each other. From a quick look, and based on [this issue](https://github.com/asottile/pyupgrade/issues/836), I'm pretty sure neither of these rules is currently in pyupgrade, so I just took the next available codes, `UP046` and `UP047`. The last main TODO, noted in the rule file and in the fixture, is to handle generic method parameters not included in the class itself, `S` in this case: ```python T = TypeVar("T") S = TypeVar("S") class Foo(Generic[T]): def bar(self, x: T, y: S) -> S: ... ``` but Alex mentioned that that might be okay to leave for a follow-up PR. I also left a TODO about handling multiple subclasses instead of bailing out when more than one is present. I'm not sure how common that would be, but I can still handle it here, or follow up on that too. I think this is unrelated to the PR, but when I ran `cargo dev generate-all`, it removed the rule code `PLW0101` from `ruff.schema.json`. It seemed unrelated, so I left that out, but I wanted to mention it just in case. ## Test Plan New test fixture, `cargo nextest run` Closes #4617, closes #12542 --------- Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
12 lines
339 B
Python
12 lines
339 B
Python
"""Replacing AnyStr requires specifying the constraints `bytes` and `str`, so
|
|
it can't be replaced if these have been shadowed. This test is in a separate
|
|
fixture because it doesn't seem possible to restore `str` to its builtin state
|
|
"""
|
|
|
|
from typing import AnyStr, Generic
|
|
|
|
str = "string"
|
|
|
|
|
|
class BadStr(Generic[AnyStr]):
|
|
var: AnyStr
|