Replies: 3 comments 2 replies
-
Currently this is not supported. There was a discussion about potentially adding Right now the most correct way is to have some attribute defined as such: class MyClass:
def __init__(self) -> None:
self.attr: int | None = None
def method(self) -> None:
self.attr = 1 # or whatever |
Beta Was this translation helpful? Give feedback.
-
I'm curious what your desired behavior would be for such an attribute? Always emit an error/warning when accessing it, but also infer the resulting type as |
Beta Was this translation helpful? Give feedback.
-
I am running into this when trying to type the open search bulk load responses. There's so many permutations of these objects that I really just wanna be able to type it as a single object type in the listwith certain attributes that may or may not exist. I can type this really easily in type script, but there seems to be zero ways to do it in Python. |
Beta Was this translation helpful? Give feedback.
-
Cross posting from Stack Overflow:
How do I do a type hint for attribute that might not exist on the object in Python?
For example:
So, concretely:
Slots may not always be involved, but I can see wanting to do something similar when specifying a
Protocol
.Beta Was this translation helpful? Give feedback.
All reactions