lichess-bot-devs / lichess-bot

A bridge between Lichess bots and chess engines
GNU Affero General Public License v3.0
715 stars 434 forks source link

Add more type hints #952

Closed AttackingOrDefending closed 1 month ago

AttackingOrDefending commented 2 months ago

Type of pull request:

Description:

Adds more type hints using TypedDict.

Related Issues:

closes #921

Checklist:

Screenshots/logs (if applicable):

AttackingOrDefending commented 2 months ago

There are 8 errors remaining.

lib\lichess.py:327: error: Incompatible types in assignment (expression has type "dict[str, Any]", variable has type "UserProfileType")  [assignment]
lib\lichess.py:397: error: Incompatible return value type (got "dict[str, Any]", expected "PublicDataType")  [return-value]
lib\engine_wrapper.py:306: error: Incompatible types in assignment (expression has type "TypedDict({'score'?: PovScore, 'pv'?: list[Move], 'depth'?: int, 'seldepth'?: int, 'time'?: float, 'nodes'?: int
, 'nps'?: int, 'tbhits'?: int, 'multipv'?: int, 'currmove'?: Move, 'currmovenumber'?: int, 'hashfull'?: int, 'cpuload'?: int, 'refutation'?: dict[Move, list[Move]], 'currline'?: dict[int, list[Move]], 'ebf'?: float, 'wdl'?: PovWdl, 'string'?: str})", variable has type "MoveInfoType")  [assignment]
lib\engine_wrapper.py:372: error: Returning Any from function declared to return "str"  [no-any-return]
lib\engine_wrapper.py:372: error: TypedDict key must be a string literal; expected one of ("Evaluation", "Winrate", "Hashfull", "Nodes", "Speed", ...)  [literal-required]
lib\engine_wrapper.py:372: error: TypedDict key must be a string literal; expected one of ("Evaluation", "Pv", "Depth", "Seldepth", "Movetime", ...)  [literal-required]
lib\engine_wrapper.py:374: error: TypedDict key must be a string literal; expected one of ("Evaluation", "Pv", "Depth", "Seldepth", "Movetime", ...)  [literal-required]
lib\engine_wrapper.py:393: error: Incompatible types in assignment (expression has type "dict[str, Any]", variable has type "ReadableMoveInfoType")  [assignment]
Found 8 errors in 2 files (checked 18 source files)

After I clean the mess in get_stats in engine_wrapper, this could be merged. I expect to end with around 4 errors that I don't think I can fix, but we could add an ignore.

EDIT: Nevermind, using cast fixed everything.

AttackingOrDefending commented 2 months ago

The only uses of Any that are left are on engine_wrapper.py on the definition of homemade engines, and on config.py on the raw dict. This is ready to be merged.

EDIT: I type hinted some part of homemade engines, but mypy isn't able to check those types. I believe they are correct. Completing engine_wrapper.py looks more promising than config.py.

MarkZH commented 1 month ago

Merge if there are no more changes.