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
this works as expected - but VScode (pylance) does not understand it and cannot add autocompletion:
while works fine with default Annotated class:
is there a way to fool pylance somehow ?
The high level idea is to make some type container that allows to set default:
T=TypeVar("T")
Vec: TypeAlias=Annotated[T, MaxLen(10)]
v1: Vec[int] # <-- will have meta max-len = 10v2: Vec[int, MaxLen(20)) # <-- will have meta max-len = 20 - but this example fails with validation error
The text was updated successfully, but these errors were encountered:
I believe what you're asking for isn't possible. Pylance doesn't know that MyAnnotated is the same as Annotation because Annotation is treated specially. Internally Pylance (actually Pyright in this case) just treats 'Annotated' as the same thing as the first type argument and ignores everything else. It doesn't do any analysis of other classes to see if they're behaving like Annotated which is what I believe would be necessary here to get the completions to work.
I think your only resolution (if you want to do this in Pylance) is to :
Use Annotated everywhere and just provide the default arguments.
Use your custom MyAnnotated but then lose completions and type information from the point of view of Pylance.
Yes, what Rich said is correct. typing.Annotated is a special form that requires special handling inside of a type checker. You can't create your own version of Annotated with similar properties without extending the type system. I recommend building on Annotated rather than trying to replace it with something custom. For example, you could use some sentinel value to represent "default value".
Hello I'm bumping my head trying to develop a custom Annotated class...
Annotated allows to add some metadata to type hint that can be checked at runtime:
so metadata is always required - but I want to develop a similar type that initialises metadata with some default value:
I'm able to make it work with this code:
this works as expected - but VScode (pylance) does not understand it and cannot add autocompletion:
while works fine with default Annotated class:
is there a way to fool pylance somehow ?
The high level idea is to make some type container that allows to set default:
The text was updated successfully, but these errors were encountered: