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

Add type annotations #377

Open
rpdelaney opened this issue Nov 17, 2021 · 1 comment
Open

Add type annotations #377

rpdelaney opened this issue Nov 17, 2021 · 1 comment

Comments

@rpdelaney
Copy link

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

Consider this class:

from steam.steamid import SteamID

from typing import Generator, List, Tuple, Union

bans: List[str] = []


class Player:
    def __init__(self, name: str, steamid_64: str) -> None:
        self.name = name
        self.steamid_64 = steamid_64

    @property
    def steamid(self) -> str:
        try:
            return SteamID(self.steamid_64).as_steam2
        except ValueError:
            return ""

    @property
    def valid_steamid(self) -> bool:
        try:
            return SteamID.is_valid(SteamID(self.steamid_64))
        except ValueError:
            return False

    @property
    def is_banned(self) -> bool:
        return self.steamid in bans

    @property
    def url(self) -> str:
        try:
            return SteamID(self.steamid_64).community_url
        except ValueError:
            return ""

    def __iter__(self) -> Generator[Tuple[str, Union[int, str]], None, None]:
        yield "name", self.name
        yield "steamid64", self.steamid_64
        yield "steamid", self.steamid
        yield "valid_steamid", self.valid_steamid
        yield "is_banned", self.is_banned
        yield "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.

@Gobot1234
Copy link

This is being worked on (see #286) but it will have to wait until V3 as Python 2 support won't be dropped until then.

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