jaraco / keyring

MIT License
1.24k stars 152 forks source link

How to implement priority with mypy? #640

Closed wwuck closed 1 year ago

wwuck commented 1 year ago

Describe the bug I'm looking at the readme https://github.com/jaraco/keyring#write-your-own-keyring-backend and it is showing in the example:

class TestKeyring(keyring.backend.KeyringBackend):
    """A test keyring which always outputs the same password
    """
    priority = 1

If I set that in my custom backend class then mypy complains with an error.

class EnvvarsKeyring(KeyringBackend):
    """Pip Environment Credentials EnvvarsKeyring."""

    priority = 1
src/keyrings/envvars/keyring.py:19: error: Incompatible types in assignment (expression has type "int", base class "KeyringBackend" defined the type as "Callable[[KeyringBackend], Union[int, float]]")  [assignment]

If I change the priority = 1 to the following:

    from jaraco.classes import properties

    @properties.ClassProperty
    @classmethod
    def priority(cls) -> float | int:  # type: ignore[override]
        """
        Priority 1.
        :returns: float | int
        """
        return 1

then I see that everything is untyped:

src/keyrings/envvars/keyring.py:8: error: Skipping analyzing "jaraco.classes": found module but no type hints or library stubs  [import]
src/keyrings/envvars/keyring.py:8: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
src/keyrings/envvars/keyring.py:58: error: Untyped decorator makes function "priority" untyped  [misc]
src/keyrings/envvars/keyring.py:60: error: Function is untyped after decorator transformation  [misc]

What is the recommendation here? Should the type signature of priority be changed? Or should I ignore the type errors on untyped jaraco.classes?

wwuck commented 1 year ago

I ended up just adding a type ignore on priority = 1.