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

Type alias fails but long version works. #10139

Closed
shadycuz opened this issue Feb 26, 2021 · 9 comments
Closed

Type alias fails but long version works. #10139

shadycuz opened this issue Feb 26, 2021 · 9 comments
Labels
bug mypy got something wrong

Comments

@shadycuz
Copy link

Bug Report

mypy fails when an alias is used but not when the long form is used

To Reproduce

  1. import botostubs
    import botostubs
  2. Create type alias at top of file but but below the import
    # Type Alias
    Vpc = botostubs.EC2.Ec2Resource.Vpc
  3. assign alias to a functions return type
    def find_vpc(vpc_name: str) -> Vpc:
  4. Run mypy
    error: Variable "my_package.utils.aws.Vpc" is not valid as a type

Expected Behavior

I expected that I would be able to use the type alias. When I use the long form.

def find_vpc(vpc_name: str) -> botostubs.EC2.Ec2Resource.Vpc:

I do not get the error when I run mypy

Actual Behavior
It errors when using the alias.

Your Environment

  • Mypy version used:
mypy                0.800
mypy-extensions     0.4.3
  • Mypy command-line flags: I just pass the directory
  • Mypy configuration options from mypy.ini (and other config files):
[mypy]

[mypy-nox.*,pytest,docker.*,git.*,boto3.*,botostubs.*,botocore.*]
ignore_missing_imports = True
  • Python version used: 3.9
  • Operating system and version: Windows 10 WSL

Not sure if it means anything but my IDE (visual studio code with pylance) works with the alias:

image

@shadycuz shadycuz added the bug mypy got something wrong label Feb 26, 2021
@JelleZijlstra
Copy link
Member

I think the issue here is that mypy doesn't know what botostubs.EC2.Ec2Resource.Vpc is and treats it as Any. If you put it in a type annotation, mypy just treats it as Any and accepts it, but if you assign it to a variable, mypy doesn't recognize you're creating an alias and just treats it as a variable.

I'm not familiar with botostubs, but it sounds like a type package, so something must be set up wrong if mypy is not picking it up.

@shadycuz
Copy link
Author

I'm not sure how it works either @JelleZijlstra but I know it shows up in my IDE as a class. You can kind of see it in my screen shot as Type[Vpc]

@jeshan can you provide some insight if you have time?

@jeshan
Copy link

jeshan commented Feb 28, 2021

@shadycuz I never used mypy and don't know how it works so I'm not sure I can be of help here. Appreciate if you can point me in the right direction.

Can you try reproducing the issue with type hints from some other package?

The way I generate code is nested classes, i.e Vpc class is in the Ec2Resource class which is itself in the EC2 one. Can you check if such a scenario work elsewhere?

If other packages work, then this issue should be moved to https://github.com/jeshan/botostubs/issues

Or maybe the real resolution is in #9404

@shadycuz
Copy link
Author

@jeshan Okay, just checking. If its just a class it should work fine with mypy.

@erictraut
Copy link

erictraut commented Mar 2, 2021

FWIW, this works fine in pyright, so it's likely a bug in mypy. The botostubs file is very large (150K lines long — appears to be auto-generated), so perhaps that's contributing to the issue.

@jeshan
Copy link

jeshan commented Mar 2, 2021

The botostubs file is very large

Yeah I should work on that some day!

But Levi wrote that at least the long form works. So, that doesn't seem to be the right explanation as mypy definitely knows about the types.

@JukkaL
Copy link
Collaborator

JukkaL commented Mar 5, 2021

@shadycuz This works for me, using the latest mypy development version and mypy 0.812. What version of botostubs are you using, and have you installed it in the same environment as mypy? For example, can you give the output of pip freeze? Also can you try the latest mypy?

@shadycuz
Copy link
Author

shadycuz commented Mar 5, 2021

botostubs and mypy are in the same environment. botostubs 0.14.1.17.15

I'm not sure if its the way mypy does versions but poetry wont update it.

poetry update mypy
Updating dependencies
Resolving dependencies... (0.7s)

No dependencies to install or update

@shadycuz
Copy link
Author

shadycuz commented Mar 5, 2021

@JukkaL That got me playing around and trying stuff and I found the issue.

I don't actually run my tests in poetry's shell, I use nox. When I run nox I only install dev dependencies. So botostubs was not getting installed. Now that I fixed that the error is gone 🎉

I also updated to 0.812 and it looks like it has additional checks because now I have more errors about other parts of my code 😊

Thanks

@shadycuz shadycuz closed this as completed Mar 5, 2021
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

5 participants