Skip to content
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

Pyright raises type error when supplying an attr field with both default and a validator #8133

Open
bavalpey opened this issue Jun 12, 2024 · 2 comments
Labels
bug Something isn't working

Comments

@bavalpey
Copy link

Describe the bug
Pyright incorrectly reports an error when supplying both a validator and a default to attrs.field
This does not seem to be a bug with the attrs package, as using mypy reports no issues.

Code or Screenshots

import attrs
@attrs.define
class Foo:
    a: bool = attrs.field(default=False, validator=attrs.validators.instance_of(bool))

image

Interestingly, if the validator is surrounded with parenthesis (which should have no effect), pyright reports no error:

import attrs
@attrs.define
class Foo:
    a: bool = attrs.field(default=False, validator=(attrs.validators.instance_of(bool)))

VS Code extension or command-line
command-line, version 1.1.367

@bavalpey bavalpey added the bug Something isn't working label Jun 12, 2024
erictraut added a commit that referenced this issue Jun 13, 2024
…n argument expression with parentheses. This addresses part of #8133.
@erictraut
Copy link
Collaborator

Thanks for the bug report.

You've actually identified two separate bugs. The first is related to your observation that the behavior changes when you add parens. As you noted, this should have no impact on type evaluation. This bug was easy to address, and the fix will be in the next release.

The second issue is more difficult to fix and is related to the way that pyright's constraint solver is designed. The constraint solver is responsible for "solving" type variables for a generic function call. The bug you've identified here is related to two other open issues: #5855 and #8048. This fix will likely take much longer. In the meantime, if you want to work around it, you can swap the order of the two arguments. Place the validator first, then default.

@bavalpey
Copy link
Author

In the meantime, if you want to work around it, you can swap the order of the two arguments. Place the validator first, then default.

This doesn't actually fix the error. I was actually fixing it by using the parenthesis. I couldn't figure out a way to fix it other than with an ignore directive.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants