Skip to content

Code that uses import arcade doesn't type check properly with mypy #537

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
SirGnip opened this issue Dec 13, 2019 · 10 comments
Closed

Code that uses import arcade doesn't type check properly with mypy #537

SirGnip opened this issue Dec 13, 2019 · 10 comments

Comments

@SirGnip
Copy link
Contributor

SirGnip commented Dec 13, 2019

Bug Report

When using mypy to type check projects that depend on arcade, mypy is unable to handle code imported from arcade.

Actual behavior:

When using mypy to type check code that imports arcade with the style in the documentation & example code, mypy reports errors like "Module has no attribute ..."

Expected behavior:

It is expected that code that relies on import arcade should type hint cleanly with mypy.

Steps to reproduce/example code:

The code snippet below uses the suggested import arcade style of importing the arcade package. It runs as expected. However, when you type check it with mypy, errors are returned:

# simple1.py
import arcade
s = arcade.Sprite()

$ mypy --strict simple1.py
simple1.py:2: error: Module has no attribute "Sprite"
Found 1 error in 1 file (checked 1 source file)

The Sprite is obviously there because the code runs as expected. mypy can't find it.

Same behavior with a slightly different import method:

# simple2.py
from arcade import Sprite
s = Sprite()

$ mypy --strict simple2.py
simple2.py:1: error: Module 'arcade' has no attribute 'Sprite'
Found 1 error in 1 file (checked 1 source file)

Interestingly, a more explicit method of importing arcade both runs and type checks properly. However, this style of importing isn't in the documentation or arcade example code:

# simple3.py
from arcade.sprite import Sprite
s = Sprite()

$ mypy --strict simple3.py
Success: no issues found in 1 source file

I'm guessing this is happening because of the from arcade.sprite import * style imports arcade does in its __init__.py flie to get everything into the arcade namespace. Maybe mypy can't follow the imports and fails? In general, I believe the * style of importing is discouraged.

The fix might be to change the from arcade.sprite import * style imports in __init__.py to from arcade.sprite import Sprite, AnimationKeyframe, AnimatedTimeBasedSprite, AnimatedWalkingSprite, get_distance_between_sprites. Obviously more verbose and a bit more to maintain. But, it is explicit.

@pvcraven
Copy link
Member

I like the idea of changing the import * statements. Might also help with issues I'm having on building docs. I'll play around with it.

@pvcraven pvcraven self-assigned this Dec 22, 2019
@pvcraven
Copy link
Member

Working on this now. Wrote a program to auto-pull out a lot of this. There's a lot of dependency stuff to slog through, and other bugs that popped up.

@SirGnip
Copy link
Contributor Author

SirGnip commented Jan 3, 2020

I fetched the current master and ran the mypy tests documented in #533 . They came back clean. So, the __init__.py restructuring seems to have made mypy happy...

@pvcraven
Copy link
Member

pvcraven commented Jan 3, 2020

Yes! Great news.

@SirGnip
Copy link
Contributor Author

SirGnip commented Jan 3, 2020

Good news / bad news. :) As I said, type checking is happy, but I just discovered runtime isn't. Complaining about No module named 'window_commands'. See the new issue I just created #546.

@pvcraven
Copy link
Member

pvcraven commented Jan 4, 2020

Yeah, I haven't figured that one out yet.

@pvcraven
Copy link
Member

pvcraven commented Jan 4, 2020

Ok, looks like I need to prepend a period.

from .window_commands import whatever

pvcraven added a commit that referenced this issue Jan 4, 2020
pvcraven added a commit that referenced this issue Jan 22, 2020
pvcraven added a commit that referenced this issue Jan 22, 2020
@pvcraven
Copy link
Member

Mypy now running clean in the codebase.

@SirGnip
Copy link
Contributor Author

SirGnip commented Jan 23, 2020

I pulled the most recent master down and mypy runs cleanly for me as well.

@pvcraven
Copy link
Member

Closed with release of 2.2.7.

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

No branches or pull requests

2 participants