Skip to content

pyright errors on incorrectly typed attrs classes, but only when used as a factory #9716

Answered by erictraut
injust asked this question in Q&A
Discussion options

You must be logged in to vote

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 the attrs 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.

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 dataclasses.field function is de…

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@injust
Comment options

@erictraut
Comment options

Answer selected by injust
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants