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

typing.TYPE_CHECKER object to change types depending on which type checker is being run #1337

Open
zunda-arrow opened this issue Jan 24, 2023 · 2 comments
Labels
topic: feature Discussions about new features for Python's type annotations

Comments

@zunda-arrow
Copy link

One issue I have run into as a library dev is varying support for typing features or bugs for both of the type checkers. typing.TYPE_CHECKER would allow me to change type definition based on the type checker so that I can support all static analysis tools more effectively.

I propose that TYPE_CHECKER would be a dict with a "name" and "version" key. Type checkers could also optionally add other keys/values.

TYPE_CHECKER = {
    "name": "type-checker-name",
    # `VersionInfo` would be implemented by each type checker to match their versioning scheme.
    "version": VersionInfo(...),
}

Using it for types would look like this:

if TYPE_CHECKER.get("name") == "pyright" and TYPE_CHECKER.get("version") > (1, 1, 290):
    T = TypeVar("T", default=int)
else:
    T = TypeVar("T")

At runtime I think the TYPE_CHECKER object should be an empty dictionary.

@zunda-arrow zunda-arrow added the topic: feature Discussions about new features for Python's type annotations label Jan 24, 2023
@srittau
Copy link
Collaborator

srittau commented Jan 24, 2023

There was a related discussion in #1129 about feature flags or a similar field.

@hmc-cs-mdrissi
Copy link

While I prefer feature flag approach so that new versions of type checker automatically enable new features, type checker name approach is still fine. I think version is too detailed though and would be maintenance pain to track.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic: feature Discussions about new features for Python's type annotations
Projects
None yet
Development

No branches or pull requests

3 participants