Cannot access instance attribute through class object #1484
Answered
by
AlexWaygood
dineshbvadhia
asked this question in
Q&A
-
The code above runs succesfully through mypy but doesn't with mypyc:
Tbh, I'm lost in the woods here :( |
Beta Was this translation helpful? Give feedback.
Answered by
AlexWaygood
Oct 4, 2023
Replies: 2 comments 3 replies
-
Since this issue is specific to mypyc, it's probably best to report it in the mypyc issue tracker rather than here (the general python/typing discussion forum). |
Beta Was this translation helpful? Give feedback.
0 replies
-
The final line of the error message mypy gives you here says:
In this case, that would mean doing this: import threading
-from typing import Any, Hashable, Type, TypeVar
+from typing import Any, Final, Hashable, Type, TypeVar
T = TypeVar("T", bound="Singleton")
class Singleton:
- __instances: dict[Any, dict[Hashable, Any]] = {} # L24
- __singleton_lock = threading.Lock() # L25
+ __instances: Final[dict[Any, dict[Hashable, Any]]] = {} # L24
+ __singleton_lock: Final = threading.Lock() # L25
__singleton_id__: Hashable
@classmethod
def _singleton_id(cls, *args: Any, **kwargs: Any) -> Hashable:
""" Return a hashable object that identifies the singleton """
return args, tuple(sorted(kwargs.items()))
@classmethod
def _instances(cls) -> dict[Hashable, Any]:
""" Return all instances of this class """
return cls.__instances.get(cls, {}).copy() # L36 |
Beta Was this translation helpful? Give feedback.
3 replies
Answer selected by
dineshbvadhia
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The final line of the error message mypy gives you here says:
In this case, that would mean doing this: