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

Context causes invalid type argument value to be inferred #360

Closed
JukkaL opened this issue Aug 3, 2014 · 1 comment
Closed

Context causes invalid type argument value to be inferred #360

JukkaL opened this issue Aug 3, 2014 · 1 comment
Labels
bug mypy got something wrong

Comments

@JukkaL
Copy link
Collaborator

JukkaL commented Aug 3, 2014

The type checker may use context to infer an invalid type variable value for a type variable such as AnyStr:

from typing import AnyStr

def f(x: AnyStr) -> AnyStr:
    if isinstance(x, str):
        return 'foo'
    else:
        return b'zar'

print(repr(f(''))) # Type argument 1 of "f" has incompatible value "object"

We should probably not use type context in cases like this when the type variable is not used within a generic type.

@JukkaL JukkaL added bug labels Aug 3, 2014
@spkersten
Copy link
Contributor

The type variable AnyStr is restricted to str or bytes. So, it seems to me that the problem here is that the return type of f is inferred to be object, which is wider than AnyStr.

I think the context should be used to restrict the possible values of the type var, instead of replacing it like seems to happen now.

So in the above example, repr expects an object, so that would not restrict AnyStr. However, in this continuation of the example:

def g(s: str): pass

g(f(''))

The context (str) would be used to restrict (and uniquely determine in this case) AnyStr to str.

I'll try to implement this.

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

No branches or pull requests

2 participants