Skip to content

Commit

Permalink
typing: allow union types for instance_of (#1385)
Browse files Browse the repository at this point in the history
* typing: allow union types for instance_of

fixes #1336

* Make hint meaningful albeit broken

* Stop running mypy on 3.9

* Declare bankruptcy

* Testing types only on 3.10+ now
  • Loading branch information
hynek authored Dec 15, 2024
1 parent 62bdbf2 commit d3f320e
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ jobs:
run: |
DO_MYPY=1
if [[ "$V" == "3.8" ]]; then
if [[ "$V" == "3.8" || "$V" == "3.9" ]]; then
DO_MYPY=0
fi
Expand Down
2 changes: 2 additions & 0 deletions changelog.d/1385.change.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
`attrs.validators.instance_of()`'s type hints now allow for union types.
For example: `instance_of(str | int)`
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ dynamic = ["version", "readme"]

[project.optional-dependencies]
tests-mypy = [
'pytest-mypy-plugins; platform_python_implementation == "CPython" and python_version >= "3.9"',
'pytest-mypy-plugins; platform_python_implementation == "CPython" and python_version >= "3.10"',
# Since the mypy error messages keep changing, we have to keep updating this
# pin.
'mypy>=1.11.1; platform_python_implementation == "CPython" and python_version >= "3.9"',
'mypy>=1.11.1; platform_python_implementation == "CPython" and python_version >= "3.10"',
]
tests = [
# For regression test to ensure cloudpickle compat doesn't break.
Expand Down
3 changes: 3 additions & 0 deletions src/attr/validators.pyi
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from types import UnionType
from typing import (
Any,
AnyStr,
Expand Down Expand Up @@ -44,6 +45,8 @@ def instance_of(
) -> _ValidatorType[_T1 | _T2 | _T3]: ...
@overload
def instance_of(type: tuple[type, ...]) -> _ValidatorType[Any]: ...
@overload
def instance_of(type: UnionType) -> _ValidatorType[Any]: ...
def optional(
validator: (
_ValidatorType[_T]
Expand Down
3 changes: 3 additions & 0 deletions tests/typing_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,9 @@ class Validated:
k: int | str | C = attr.ib(
validator=attrs.validators.instance_of((int, C, str))
)
kk: int | str | C = attr.ib(
validator=attrs.validators.instance_of(int | C | str)
)

l: Any = attr.ib(
validator=attr.validators.not_(attr.validators.in_("abc"))
Expand Down
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ min_version = 4
env_list =
pre-commit,
py3{8,9,10,11,12,13}-tests,
py3{9,10,11,12,13}-mypy,
py3{10,11,12,13}-mypy,
pypy3,
pyright,
docs{,-sponsors},
Expand Down

0 comments on commit d3f320e

Please sign in to comment.