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

typehints (label) - mypy #451

Closed
JaskRendix opened this issue Feb 26, 2023 · 2 comments
Closed

typehints (label) - mypy #451

JaskRendix opened this issue Feb 26, 2023 · 2 comments

Comments

@JaskRendix
Copy link

JaskRendix commented Feb 26, 2023

Hello, there!

I was running mypy to check for typehints (spring cleaning) and it flagged me the labels (with .translate)
-> no issues with buttons and images.

Out of curiosity I tried to run mypy on this file:
https://github.com/ppizarror/pygame-menu/blob/master/pygame_menu/examples/other/widget_positioning.py

the output with pygame_menu 4.3.8:

.../widget_positioning.py:28: error: Item "List[Label]" of "Union[Label, List[Label]]" has no attribute "translate"
.../widget_positioning.py:40: error: Item "List[Label]" of "Union[Label, List[Label]]" has no attribute "rotate"
.../widget_positioning.py:41: error: Item "List[Label]" of "Union[Label, List[Label]]" has no attribute "translate"
.../states/combat/widget_positioning.py:74: error: Argument 1 to "pack" of "Frame" has incompatible type "Union[Label, List[Label]]"; expected "Union[Widget, List[Widget], Tuple[Widget, ...]]"

that's the exact output I get on my repository.

@ppizarror
Copy link
Owner

ppizarror commented Feb 28, 2023

Hi! I didn't know about mypy and found dozens of errors. However, most are related to Optional[...]. For example, there are around 50 issues like "None YYY doesn't have XXX method...." in methods that should not executed if the YYY variable is None; thus, most belong to static analysis issues.

I'll try to solve all (I pushed a new commit a few hours ago) in the next few days.

@ppizarror
Copy link
Owner

I think that static analysis does not work very well. In the case you provided, menu.add.label returns a list of labels if max_char is enabled; thus, translate and rotate are not defined.

I've reviewed dozens of mypy issues, and most are invalid. I'm open to any PR addressing more "issues," but this is towards sanitizing mypy rather than improving the menu typing. For example, mypy says that baseimage has the following errors at line 880:

...
pygame_menu\baseimage.py:838: error: Item "None" of "Optional[Rect]" has no attribute "width"  [union-attr]
pygame_menu\baseimage.py:838: error: Item "None" of "Optional[Rect]" has no attribute "height"  [union-attr]
pygame_menu\baseimage.py:839: error: Item "None" of "Optional[Rect]" has no attribute "width"  [union-attr]
pygame_menu\baseimage.py:839: error: Item "None" of "Optional[Rect]" has no attribute "height"  [union-attr]
pygame_menu\baseimage.py:850: error: Item "None" of "Optional[Rect]" has no attribute "width"  [union-attr]
pygame_menu\baseimage.py:865: error: Item "None" of "Optional[Rect]" has no attribute "height"  [union-attr]
pygame_menu\baseimage.py:880: error: Item "None" of "Optional[Rect]" has no attribute "width"  [union-attr]
pygame_menu\baseimage.py:881: error: Item "None" of "Optional[Rect]" has no attribute "height"  [union-attr]
pygame_menu\baseimage.py:896: error: Item "None" of "Optional[Rect]" has no attribute "width"  [union-attr]
pygame_menu\baseimage.py:896: error: Item "None" of "Optional[Rect]" has no attribute "height"  [union-attr]
...

However, within baseimage.draw the "if area is None" is already addressed; thus, these mypy issues are not valid.

if area is None:
area = surface.get_rect()

For these reasons, I will close this issue for now. Anyone with typing improvements can create as many PRs as they want. Thanks!

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