-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
Warn when PEP695 type param clashes with a TypeVar name #18499
Comments
The correct syntax is either class Box[TAmount: (int, Decimal)]:
... or from typing import Generic, TypeVar
TAmount = TypeVar("TAmount", int, Decimal)
class Box(Generic[TAmount]):
... We may consider adding an optional warning when a PEP695 type parameter name clashes with the name of a globally defined typevar. |
Proposed title: Warn when PEP695 type param clashes with a TypeVar name |
@sterliakov: Thanks for that - you are entirely right! Should I change the ticket up to track a feature request now, or just close it (I'm happy with either)? |
I think this proposal is sound, let's keep this as a feature request - please just update the title to match the core idea. |
@sterliakov Could I check if the following is also an example of mixing the syntaxes incorrectly? from decimal import Decimal
class Box[TAmount: (int, Decimal)]:
def __init__(self, amount: TAmount) -> None:
self.amount: TAmount = amount
def non_generic_function(amount: int | float) -> Box:
# This does not error
return Box(amount)
def generic_function[SAmount: (int, float)](amount: SAmount) -> Box[SAmount]:
# This correctly errors
return Box(amount)
# This correctly errors as float is not an allowed type
my_instance = Box(1.5) https://mypy-play.net/?mypy=master&python=3.12&gist=ddc7e7bb4d76aab148657cf95d8a70a0 In particular, if you declare a generic class, do you also need to declare all functions which build instances of that class as generic in order to get them to check the types being passed to the constructor? |
@leamingrad not exactly, the problem in your snippet is that bare In the following snippet def non_generic_function(amount: int | float) -> Box:
Box(amount) # This emits a diagnostic
return Box(amount) # This does not I highly recommend running So yes, builders should either be generic or return a concrete subtype (e.g. |
Thanks for that - I completely missed that in the docs (it is covered in https://mypy.readthedocs.io/en/stable/generics.html#generic-type-aliases but I missed the relevant part).
I completely agree. Unfortunately the codebase I work on doesn't have |
Bug Report
When defining a generic class with a type variable bounded to number classes, arithmetic operations are not available in class methods.
To Reproduce
Playground link: https://mypy-play.net/?mypy=master&python=3.12&gist=97147ba435ef98e5566e5c6a01424f92
Sample program
Expected Behavior
The program should typecheck successfully.
Actual Behavior
This seems to only be a problem in methods on the generic class. The functions which operate on values and class instances typecheck cleanly.
Note that the
TypeVar
is defined as being exactlyint
orDecimal
as per #12899 (comment).The text was updated successfully, but these errors were encountered: