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

TypeVar Support #119

Open
seandstewart opened this issue Jul 17, 2020 · 0 comments
Open

TypeVar Support #119

seandstewart opened this issue Jul 17, 2020 · 0 comments
Projects

Comments

@seandstewart
Copy link
Owner

  • typical version: 2.0.25
  • Python version: 3.8.1
  • Operating System: Unix

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

  1. 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`
if use.__class__ is TypeVar:
    if use.__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.

  1. 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:
import typic
from typing import Generic, TypeVar, get_type_hints
import dataclasses
T = TypeVar("T")
@dataclasses.dataclass
class Foo(Generic[T]):
    bar: T

FooStrT = 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) for name, t in get_type_hints(FooStrT)}
@seandstewart seandstewart added this to To do in v2.6 via automation Jan 5, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
v2.6
To do
Development

No branches or pull requests

1 participant