lykoss / lykos

Werewolf, the popular detective/social party game (a theme of Mafia)
https://werewolf.chat
Other
127 stars 63 forks source link

Update type annotations to the point where mypy is satisfied #470

Closed iamgreaser closed 3 years ago

iamgreaser commented 3 years ago

NOTE: Testingwise I ended up running this and doing a !join then a !quit, although that was after the first commit and I missed one from __future__ import annotations line so I needed another commit. This does touch a lot of files and I'm not sure how you want this PR to be arranged.

If a botconfig.py is not provided then running mypy like this (available on pip) should pass:

python3 -m mypy .

This is good enough to get mypy 0.812 to typecheck things successfully in non-strict mode, although a custom mypy.ini was provided to ignore all failures in src.messages.*.

A large chunk of type comments were turned into Python 3.5/3.6 inline annotations.

from __future__ import annotations allows Python 3.7+ to not actually attempt to resolve type names when it sees said inline annotations.

Various imports are guarded by the typing.TYPE_CHECKING variable so not to introduce potential import loops into the code.

Several assertions were put in places to make mypy happy. In most cases, if the assertions were not there, the code would have crashed anyway.

UserSet and UserList were confirmed to not need to be generic. UserDict on the other hand seemed like the key or the value could be not a User and therefore both key and value needed to be generic.

Several cases of users.User, mostly those in annotations and type comments, were changed to be just User.

src/settings.py was for the purposes of this commit deemed to be something longing to be killed with fire and as a result I've largely avoided giving it proper type annotations for some things, especially where it gets extra stuff dumped into it for no reason from src/wolfgame.py.

A bug was fixed in src/status/dying.py as a result of needing to get the types checked. None is falsy, but it was meant to return True rather than do a no-value return (which returns None). I'm not sure if we want to split this one out or not. (The other case of a no-value return being changed was due to it actually needing to return None, so the change is behaviourally-identical.)