Skip to content

Add type annotations #377

Description

@rpdelaney

Is your feature request related to a problem? Please describe.

Consider this class:

fromsteam.steamidimportSteamIDfromtypingimportGenerator, List, Tuple, Unionbans: List[str] = []
classPlayer:
def__init__(self, name: str, steamid_64: str) ->None:
self.name=nameself.steamid_64=steamid_64@propertydefsteamid(self) ->str:
try:
returnSteamID(self.steamid_64).as_steam2exceptValueError:
return""@propertydefvalid_steamid(self) ->bool:
try:
returnSteamID.is_valid(SteamID(self.steamid_64))
exceptValueError:
returnFalse@propertydefis_banned(self) ->bool:
returnself.steamidinbans@propertydefurl(self) ->str:
try:
returnSteamID(self.steamid_64).community_urlexceptValueError:
return""def__iter__(self) ->Generator[Tuple[str, Union[int, str]], None, None]:
yield"name", self.nameyield"steamid64", self.steamid_64yield"steamid", self.steamidyield"valid_steamid", self.valid_steamidyield"is_banned", self.is_bannedyield"url", self.url

Because the instance methods in the steam module are untyped, mypy gives errors:

foo.py:16:13: error: Returning Any from function declared to return "str" [no-any-return]
foo.py:23:13: error: Returning Any from function declared to return "bool" [no-any-return]
foo.py:34:13: error: Returning Any from function declared to return "str" [no-any-return]
Found 3 errors in 1 file (checked 1 source file)

To silence the warnings, I must coerce the returned expressions:

diff --git i/foo.py w/foo.py
index f1f6722..d15cbb6 100644
--- i/foo.py+++ w/foo.py@@ -13,14 +13,14 @@ class Player:
@property
def steamid(self) -> str:
try:
- return SteamID(self.steamid_64).as_steam2+ return str(SteamID(self.steamid_64).as_steam2)
except ValueError:
return ""
@property
def valid_steamid(self) -> bool:
try:
- return SteamID.is_valid(SteamID(self.steamid_64))+ return bool(SteamID.is_valid(SteamID(self.steamid_64)))
except ValueError:
return False
@@ -31,7 +31,7 @@ class Player:
@property
def url(self) -> str:
try:
- return SteamID(self.steamid_64).community_url+ return str(SteamID(self.steamid_64).community_url)
except ValueError:
return ""

Describe the solution you'd like

  • steam should return typed values.

This would aid consumers in type checking their code that depends on steam.

  • Stretch goal: add type checking to this repository.

That would guard against returning wrong types as the module develops.

Describe alternatives you've considered

  • Do nothing.

The easiest option. It's not so inconvenient to coerce or annotate types in my code. But the tools are available in python, and I think it's worth considering.

Thanks for reading. Happy to answer questions.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions