-
I ran into this seemingly inconsistent behaviour after incorrectly changing some attrs converters in agronholm/apscheduler#1014 (I forgot that the converters needed to account for a default value of Minimal repro: from attrs import Factory, define, field
def _int_to_int(value: int) -> int:
return value
@define
class Foo1:
# Incorrect because `None` is not an `int`
one: int = field(default=None)
@define
class Foo2:
# Incorrect because the converter does not take `None`
one: int | None = field(default=None, converter=_int_to_int)
foo1 = Foo1()
foo2 = Foo2()
# pyright only errors when they are used as an attrs factory
@define
class Bar1:
foo1: Foo1 = Factory(Foo1)
@define
class Bar2:
foo2: Foo2 = Factory(Foo2)
My environment:
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
It's important to understand that pyright doesn't have any specific knowledge of Interestingly, if I switch your code to use the stdlib dataclass rather than attrs, both pyright and mypy detect that there's a mismatch in the default type. from dataclasses import dataclass, field
@dataclass
class Foo:
x: int = field(default=None) # Error: None is not assignable to int This error is detected because of the way that the def field(
*,
default: _T,
...
) -> _T: ... By contrast, @overload
def field(
*,
default: None = ...,
...
) -> Any: ... The return type for the call is evaluated as According to the So, I think pyright is working correctly here. Not surprisingly, mypy's behavior matches pyright's. Both pyright and mypy pass the type checker conformance tests for this functionality. |
Beta Was this translation helpful? Give feedback.
It's important to understand that pyright doesn't have any specific knowledge of
attrs
or its behaviors. Pyright performs its analysis based on the type information provided by theattrs
library, which uses the dataclass_transform facility of the Python typing spec along with the recently-added support for converters.Interestingly, if I switch your code to use the stdlib dataclass rather than attrs, both pyright and mypy detect that there's a mismatch in the default type.
This error is detected because of the way that the
dataclasses.field
function is de…