You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Typical currently stumbles over classes which make use of TypeVar in their definitions, such as Generics. At the very least, we should be able to treat anonymous (unbound) typevars as "Any", and at best, map bound TypeVars correctly.
Implementation
Implement TypeVar resolution, perhaps in Resolver.annotation, to determine whether this should be treated as a Union or Any. If the TypeVar is constrained, treat it as a Union for resolution, otherwise, an Any:
# If done as a branch in `Resolver.annotation`ifuse.__class__isTypeVar:
ifuse.__constraints__:
use=Union[*use.__constraints__]
else:
use=Any
For consideration - do we want to create a NewType for this Union?
We probably also want to retain a flag on the ResolvedAnnotation that signals this came from a TypeVar.
Implement resolution for Generics - this will take additional experimentation, and will likely need to tweak internal funcsigs. The easiest way to pre-emptively map subscripted generics to their given bound types:
importtypicfromtypingimportGeneric, TypeVar, get_type_hintsimportdataclassesT=TypeVar("T")
@dataclasses.dataclassclassFoo(Generic[T]):
bar: TFooStrT=Foo[str]
bindings=dict(zip(Foo.__parameters__, FooStrT.__args__))
# If no bindings, our TypeVar resolution works for us. Otherwise, we should override the TypeVar anno with the binding.hints= {name: bindings.get(t, t) forname, tinget_type_hints(FooStrT)}
The text was updated successfully, but these errors were encountered:
Description
Typical currently stumbles over classes which make use of TypeVar in their definitions, such as Generics. At the very least, we should be able to treat anonymous (unbound) typevars as "Any", and at best, map bound TypeVars correctly.
Implementation
Resolver.annotation
, to determine whether this should be treated as a Union or Any. If the TypeVar is constrained, treat it as a Union for resolution, otherwise, an Any:For consideration - do we want to create a NewType for this Union?
We probably also want to retain a flag on the
ResolvedAnnotation
that signals this came from a TypeVar.The text was updated successfully, but these errors were encountered: