-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
Unsoundness from unbound type variables in attributes #6520
Comments
This is a consequence of the fact that we allow methods to be generic "on their own", i.e. this is perfectly valid:
But then there is #2885. Note there is similar (but not directly related) #3172 that proposes to reject use of class type variables in method bodies of static methods. In general, we might want to perform some overhaul of type variable scopes and clearly document the rules. Currently it often works how it works. |
It seems more that the problem is the treatment of the type variable's interaction with the attribute? |
Yes, probably the simplest solution would be to prohibit storing such type on In general, as I already said we might want to re-think how type variable binding works in general (maybe at least store a pointer to the binder for every |
It turns out this is a duplicate of an older issue #1903, but I am going to close the other one, since it has less discussion. |
Apologies if this is off-topic, but is there any way to use How would one go about doing something like this? TypeForAPI = TypeVar('TypeForAPI')
TypeForDB = TypeVar('TypeForDB')
class Parser:
type_for_api: TypeForAPI = str
type_for_db: TypeForDB = str
@classmethod
def dump_to_db(cls, value: TypeForAPI) -> TypeForDB:
return cls.type_for_db(value)
@classmethod
def dump_to_api(cls, value: TypeForDB) -> TypeForAPI:
return cls.type_for_api(value)
class NumberParser(Parser):
type_for_db: TypeForDB = float
type_for_api: TypeForAPI = Decimal
class DateParser(Parser):
type_for_db: TypeForDB = datetime.date
type_for_api: TypeForAPI = str
@classmethod
def dump_to_db(cls, value: TypeForAPI) -> TypeForDB:
return dateutil.parse(value).date()
@classmethod
def dump_to_api(cls, value: TypeForDB) -> TypeForAPI:
return value.isoformat() Appears to be superficially related to #3625 (I'm having the same issue with typevars on metaclasses elsewhere in the same file that example was taken from ^, but it could be a red herring). |
Minimized from some real code at dropbox exhibiting this failure in a nasty way.
The following code typechecks:
Here,
T
is not bound in the class but ends up in the attribute, where it is then matched with the unbound return type variable in get.(Interestingly, it also works if
get
returns a different unbound type variable)A reveal type reports:
Revealed type is 'T`-1'
The text was updated successfully, but these errors were encountered: