You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am attempting to contribute an implementation using this library to a type checked project and mypy is failing the PR.
I think the cause of the failure is that the backend returned from keyring.get_keyring() is typed as the wrong class. It's being cast to keyring.backend.Backend but it is in fact a subclass.
Type checkers reject this incorrect casting whenever a method is used that's unique to a subclass, such as get_preferred_collection() which exists on keyring.backends.SecretService.Keyring but not keyring.backend.KeyringBackend.
pydantic_settings/sources.py:666: error: Incompatible types in assignment (expression has type "KeyringBackend", variable has type "Keyring") [assignment]
pydantic_settings/sources.py:676: error: Call to untyped function "get_preferred_collection" in typed context [no-untyped-call]
Found 2 errors in 1 file (checked 5 source files)
It looks tricky but maybe I can help make some progress on these type annotations, maybe the cast helps identify a place to start?
I can't figure out if there is already a mypy workflow in use in this project (is pytest-mypy called somewhere?)
The text was updated successfully, but these errors were encountered:
lmmx
changed the title
Type error due to typing.cast as a subtype
Type error due to typing.cast of backend.KeyringBackend on a subtype
Aug 12, 2023
lmmx
changed the title
Type error due to typing.cast of backend.KeyringBackend on a subtype
Type checking error due to casting a subclass as backend.KeyringBackend base class
Aug 12, 2023
Thanks for the report and the offer to help. Yes, mypy checks are run as part of the test suite (via pytest-mypy). Simply run tox and it will execute the test suite including any mypy checks. Run tox -- -k mypy to run only the mypy tests. I just ran them and they are in fact failing in main, so probably the failing tests need to be addressed first.
Describe the bug
I am attempting to contribute an implementation using this library to a type checked project and mypy is failing the PR.
I think the cause of the failure is that the backend returned from
keyring.get_keyring()
is typed as the wrong class. It's being cast tokeyring.backend.Backend
but it is in fact a subclass.keyring/keyring/core.py
Lines 30 to 34 in af72339
Type checkers reject this incorrect casting whenever a method is used that's unique to a subclass, such as
get_preferred_collection()
which exists onkeyring.backends.SecretService.Keyring
but notkeyring.backend.KeyringBackend
.due to this code at line 666 👿 :
and further on at 676:
It looks tricky but maybe I can help make some progress on these type annotations, maybe the cast helps identify a place to start?
I can't figure out if there is already a mypy workflow in use in this project (is
pytest-mypy
called somewhere?)The text was updated successfully, but these errors were encountered: