-
-
Notifications
You must be signed in to change notification settings - Fork 400
Improve type hinting of or_ #1471
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
Conversation
for more information, see https://pre-commit.ci
Added a new TypeVar '_U' bound to UnionType for type validation.
I believe that I need to add some mypy tests to adequately verify that or_ works the way that I believe it does. However, it's unclear where I should add that. |
That would go to tests/typing_example.py – just exercise the types (it doesn't have to be runnable) – thanks! |
So I can't make this fail on Mypy… maybe its plugin somehow works around this? Have you had any failures somehow somewhere? |
OK, this fails on Pyright: @attrs.define
class ValidatedInconsistenOr:
num: int = attrs.field(
validator=attrs.validators.or_(
# Various types of validators.
attrs.validators.ge(0),
attrs.validators.instance_of(object),
)
) I'll split up the type hints a bit. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
Summary
The type hinting for the or_ function presumes that all validators used have the same input type though that is not a requirement. This adds several overloads and a fall-through Any type catch-all to the or_ function that will help satisfy the type check.
Pull Request Check List
main
branch – use a separate branch!Our CI fails if coverage is not 100%.
.pyi
).tests/typing_example.py
.attr/__init__.pyi
, they've also been re-imported inattrs/__init__.pyi
.docs/api.rst
by hand.@attr.s()
and@attrs.define()
have to be added by hand too.versionadded
,versionchanged
, ordeprecated
directives.The next version is the second number in the current release + 1.
The first number represents the current year.
So if the current version on PyPI is 22.2.0, the next version is gonna be 22.3.0.
If the next version is the first in the new year, it'll be 23.1.0.
attrs.define()
andattr.s()
, you have to add version directives to both..rst
and.md
files is written using semantic newlines.changelog.d
.