ppizarror / pygame-menu

A menu for pygame. Simple, and easy to use
https://pygame-menu.readthedocs.io/
Other
555 stars 141 forks source link

typehints (label) - mypy #451

Closed JaskRendix closed 1 year ago

JaskRendix commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

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.

https://github.com/ppizarror/pygame-menu/blob/39a6e92426cd63de7b4526b8ad2e2cd33de66d7c/pygame_menu/baseimage.py#L814-L815

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