Skip to content

Importing generic type aliases #2887

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

Closed
dmzkrsk opened this issue Feb 21, 2017 · 5 comments
Closed

Importing generic type aliases #2887

dmzkrsk opened this issue Feb 21, 2017 · 5 comments
Assignees
Labels

Comments

@dmzkrsk
Copy link

dmzkrsk commented Feb 21, 2017

lib.py:

from typing import Callable, TypeVar, Tuple

T = TypeVar('T')
R = TypeVar('R')

Transform = Callable[[T, int], Tuple[T, R]]

main.py:

from typing import Tuple
from lib import Transform


def int_tf(m: int) -> Transform[int, str]:  # Bad number of arguments for type alias, expected: 0, given: 2
    def transform(i: int, pos: int) -> Tuple[int, str]:
        a = (i + pos) % m  # Name 'T' is not defined Name 'R' is not defined
        return (a, 'ok')

    return transform

It works when an alias is defined in the same file

@dmzkrsk
Copy link
Author

dmzkrsk commented Feb 21, 2017

Looks like T and R are not "bound" to Transform

from lib import T, R solves this issue (but it doesn't look right)
Defining T and R in main.py works too

@ilevkivskyi
Copy link
Member

Thanks for reporting this! Indeed, this looks like a bug. I will take a look at this.

@ilevkivskyi ilevkivskyi self-assigned this Mar 31, 2017
@JukkaL
Copy link
Collaborator

JukkaL commented May 31, 2017

@ilevkivskyi Are you still working on this?

@ilevkivskyi
Copy link
Member

Sorry, last weeks were busy, so I could not finish this. I actually wanted to spend next week less on reviewing PRs and more on fixing old assigned issues. Do you want to work on this one?

@JukkaL
Copy link
Collaborator

JukkaL commented May 31, 2017

I'm reviewing github issues and trying keep the information about who's working on what up-to-date. This issue isn't particularly urgent among the high-priority issues.

JukkaL pushed a commit that referenced this issue Jul 7, 2017
Fixes #2887. Fixes #3191.

In addition this prohibits reassigning aliases. Previously something like 
this was allowed:

```
if random():
    Alias = Sequence[int]
else:
    Alias = Sequence[float]

def fun(arg: Alias) -> None:
    ...
```

Now this will generate an error: 

    Cannot assign multiple types to name "Alias" without an explicit "Type[...]" 
    annotation. 

See #3494 for background.

Finally, this simplifies the logic in semanal.py, so that most processing of type aliases 
happens in one function.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants