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

"Invalid type" error when creating a synonym for Optional[int] #1637

Closed
rwbarton opened this issue Jun 3, 2016 · 8 comments
Closed

"Invalid type" error when creating a synonym for Optional[int] #1637

rwbarton opened this issue Jun 3, 2016 · 8 comments

Comments

@rwbarton
Copy link
Contributor

rwbarton commented Jun 3, 2016

from typing import Optional
MyType = Optional[int]
def f(x: bool) -> MyType:
    pass

produces an error

x/invalidtype.py: note: In function "f":
x/invalidtype.py:4: error: Invalid type "invalidtype.MyType"

A type synonym for Union[str, int] works fine.

@gvanrossum
Copy link
Member

IIRC global assignments are only remembered as type aliases when they contain certain things. Maybe Optional isn't in the list of things it looks for, while Union is? (I've occasionally had a similar issue but always just worked around it.)

@rwbarton
Copy link
Contributor Author

rwbarton commented Jun 5, 2016

Yep that's exactly it. PR incoming...

@rwbarton
Copy link
Contributor Author

rwbarton commented Jun 5, 2016

Actually this turns out to be more subtle than expected because of the way that Optional[T] reduces to simply T. After strict optional checking lands this won't be an issue, so let's just defer this until then.

@rwbarton
Copy link
Contributor Author

rwbarton commented Jun 5, 2016

Wait, that plan only makes sense if we are getting rid of non-strict optional checking, which probably won't happen that soon.

I managed to get the tests passing again with some simple changes but I don't entirely understand the intent of the old code, so hopefully @JukkaL can take a quick look.

@gvanrossum
Copy link
Member

Fixed by 2050d80.

@pgjones
Copy link

pgjones commented Apr 17, 2017

I don't think this is fixed, both the above and the snippet below fail with mypy 0.501 on Python 3.5.2, as discussed there is no error with --strict-optional enabled.

from typing import List, Optional

T = Optional[int]

Ts = []  # type: List[T]

mypy test.py gives test.py:5: error: Invalid type "test.T".

@ilevkivskyi
Copy link
Member

ilevkivskyi commented Apr 17, 2017

I don't think this is fixed

Confirmed, although I am not sure this is the original issue. In fact this has nothing to do with --strict-optional, a simplest repro that fails both with and without --strict-optional:

A = Union[int]
x: A

The reason is how aliases are analyzed, there is a logical flaw in code, it naively expects that if alias is an IndexExpr then it should result in a parameterized type.

Sorry, I don't have time to fix this right now, although the fix should be quite simple.

@gvanrossum
Copy link
Member

Please open a new issue since Ivan says it's not the same issue.

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

4 participants