-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Generic type not handled correctly in dataclass attribute with converter #9512
Comments
Yo dit al geprobeerd? De verschillen in de types die worden onthuld door reveal_type kunnen worden verklaard door hoe attrs en het converter-argument werken in combinatie met type-inferentie in Python. Probleemanalyse: In de definitie van Base, wordt data: set[T] rechtstreeks toegewezen. Python (en attrs) kan type-informatie direct afleiden uit de initiële waarde die wordt meegegeven tijdens het instantiëren. Hierdoor kan Base({1, 2}) correct worden geïnterpreteerd als Base[int], en hetzelfde geldt voor Base({1, 2}).data. Bij BaseConverted, wordt de data-waarde verwerkt door field(converter=set), wat betekent dat alle invoer (bijvoorbeeld een lijst [1, 2]) wordt omgezet naar een set door de converterfunctie. python @Frozen Samenvatting: |
This comment has been minimized.
This comment has been minimized.
Hi! Thanks for the replies @CrzyHAX91 @loing-cdut . No, using a lambda or any other properly annotated function doesn't work for me either (and for the record, from collections.abc import Iterable
from typing import Generic, TypeVar
from attrs import field, frozen
from typing_extensions import reveal_type
T = TypeVar("T")
@frozen
class Base(Generic[T]):
data: set[T] = field()
def to_set(data: Iterable[T]) -> set[T]:
return set(data)
@frozen
class BaseConverted(Generic[T]):
data: set[T] = field(converter=to_set)
reveal_type(Base({1, 2})) # Type of "Base(1, 2)" is "Base[int]"
reveal_type(Base({1, 2}).data) # Type of "Base(1, 2).data" is "set[int]"
reveal_type(BaseConverted([1, 2])) # Type of "BaseConverted([1, 2])" is "BaseConverted[int]"
reveal_type(BaseConverted([1, 2]).data) # Type of "BaseConverted([1, 2]).data" is "set[T@BaseConverted]" In fact, the lambda version even leads to a regression: @frozen
class BaseConverted(Generic[T]):
data: set[T] = field(converter=lambda x: set(x))
reveal_type(BaseConverted([1, 2])) # Type of "BaseConverted([1, 2])" is "BaseConverted[Unknown]"
reveal_type(BaseConverted([1, 2]).data) # Type of "BaseConverted([1, 2]).data" is "set[T@BaseConverted]" so, pyright can't even figure out Have you tried your modified version? For the record: I'm on
|
This is addressed in pyright 1.1.390. |
Wow, fantastic, thanks so much for the quick fix 👏 ! |
This bug report comes by way of this post from @Darkdragon84.
The text was updated successfully, but these errors were encountered: