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

Warn about unconstrained typevars #3254

Closed
JelleZijlstra opened this issue Apr 26, 2017 · 3 comments
Closed

Warn about unconstrained typevars #3254

JelleZijlstra opened this issue Apr 26, 2017 · 3 comments

Comments

@JelleZijlstra
Copy link
Member

I think code like this should produce an error:

def f(x: AnyStr) -> None: ...

because there's nothing to constrain the type variable. There is a good amount of code like this in typeshed.

I'm not sure how easy this would be to implement. Maybe it should be behind a flag at first; I imagine this proposed change may lead to a lot of new errors, but it's a good way to educate people about what typevars are for.

@JukkaL
Copy link
Collaborator

JukkaL commented Apr 26, 2017

The example is actually okay -- the type of an argument at a call site will be used to infer a value for the type variable. It's not recommended since Union[str, bytes] or similar is more general, as it also accepts unions, and it allows mypy to generate better error messages. Arguably mypy could be improved to make the AnyStr version as good as the union version, but there are a few open issues that would need to be fixed first (#963 and #1943).

@JelleZijlstra
Copy link
Member Author

I guess that depends on what you consider OK; the example doesn't compromise type safety or create false positive errors, but it's using a harder-to-understand part of the type system (type variables) when a simpler solution (unions) is available and should have the same results.

That line of argument applies even more strongly if you use a typevar without bounds, like this:

T = TypeVar('T')

def f(x: T) -> None: ...

@JelleZijlstra
Copy link
Member Author

This is essentially the same as #2885 except that it talks about argument rather than return types, so closing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants