Skip to content

Commit d3f320e

Browse files
authored
typing: allow union types for instance_of (#1385)
* 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
1 parent 62bdbf2 commit d3f320e

File tree

6 files changed

+12
-4
lines changed

6 files changed

+12
-4
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ jobs:
6666
run: |
6767
DO_MYPY=1
6868
69-
if [[ "$V" == "3.8" ]]; then
69+
if [[ "$V" == "3.8" || "$V" == "3.9" ]]; then
7070
DO_MYPY=0
7171
fi
7272

changelog.d/1385.change.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
`attrs.validators.instance_of()`'s type hints now allow for union types.
2+
For example: `instance_of(str | int)`

pyproject.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,10 @@ dynamic = ["version", "readme"]
3030

3131
[project.optional-dependencies]
3232
tests-mypy = [
33-
'pytest-mypy-plugins; platform_python_implementation == "CPython" and python_version >= "3.9"',
33+
'pytest-mypy-plugins; platform_python_implementation == "CPython" and python_version >= "3.10"',
3434
# Since the mypy error messages keep changing, we have to keep updating this
3535
# pin.
36-
'mypy>=1.11.1; platform_python_implementation == "CPython" and python_version >= "3.9"',
36+
'mypy>=1.11.1; platform_python_implementation == "CPython" and python_version >= "3.10"',
3737
]
3838
tests = [
3939
# For regression test to ensure cloudpickle compat doesn't break.

src/attr/validators.pyi

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from types import UnionType
12
from typing import (
23
Any,
34
AnyStr,
@@ -44,6 +45,8 @@ def instance_of(
4445
) -> _ValidatorType[_T1 | _T2 | _T3]: ...
4546
@overload
4647
def instance_of(type: tuple[type, ...]) -> _ValidatorType[Any]: ...
48+
@overload
49+
def instance_of(type: UnionType) -> _ValidatorType[Any]: ...
4750
def optional(
4851
validator: (
4952
_ValidatorType[_T]

tests/typing_example.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,9 @@ class Validated:
228228
k: int | str | C = attr.ib(
229229
validator=attrs.validators.instance_of((int, C, str))
230230
)
231+
kk: int | str | C = attr.ib(
232+
validator=attrs.validators.instance_of(int | C | str)
233+
)
231234

232235
l: Any = attr.ib(
233236
validator=attr.validators.not_(attr.validators.in_("abc"))

tox.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ min_version = 4
33
env_list =
44
pre-commit,
55
py3{8,9,10,11,12,13}-tests,
6-
py3{9,10,11,12,13}-mypy,
6+
py3{10,11,12,13}-mypy,
77
pypy3,
88
pyright,
99
docs{,-sponsors},

0 commit comments

Comments
 (0)