Skip to content

Commit df4cf48

Browse files
committed
Validate callable declarations across Python versions
1 parent 5622199 commit df4cf48

5 files changed

Lines changed: 48 additions & 20 deletions

File tree

‎.github/workflows/ci.yml‎

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ on:
77
branches: [ main, master, develop ]
88
workflow_dispatch:
99

10+
permissions:
11+
contents: read
12+
1013
jobs:
1114
test:
1215
name: Test Python ${{ matrix.python-version }} on ${{ matrix.os }}
@@ -19,10 +22,10 @@ jobs:
1922

2023
steps:
2124
- name: Checkout code
22-
uses: actions/checkout@v4
25+
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
2326

2427
- name: Set up Python ${{ matrix.python-version }}
25-
uses: actions/setup-python@v5
28+
uses: actions/setup-python@5fda3b95a4ea91299a34e894583c3862153e4b97 # v7.0.0
2629
with:
2730
python-version: ${{ matrix.python-version }}
2831
cache: 'pip'
@@ -37,7 +40,7 @@ jobs:
3740
pytest tests/ -v --cov=python_introspect --cov-report=xml --cov-report=term
3841
3942
- name: Upload coverage to Codecov
40-
uses: codecov/codecov-action@v4
43+
uses: codecov/codecov-action@fb8b3582c8e4def4969c97caa2f19720cb33a72f # v7.0.0
4144
if: matrix.os == 'ubuntu-latest' && matrix.python-version == '3.12'
4245
with:
4346
file: ./coverage.xml
@@ -50,10 +53,10 @@ jobs:
5053

5154
steps:
5255
- name: Checkout code
53-
uses: actions/checkout@v4
56+
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
5457

5558
- name: Set up Python
56-
uses: actions/setup-python@v5
59+
uses: actions/setup-python@5fda3b95a4ea91299a34e894583c3862153e4b97 # v7.0.0
5760
with:
5861
python-version: '3.11'
5962
cache: 'pip'
@@ -85,10 +88,10 @@ jobs:
8588

8689
steps:
8790
- name: Checkout code
88-
uses: actions/checkout@v4
91+
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
8992

9093
- name: Set up Python
91-
uses: actions/setup-python@v5
94+
uses: actions/setup-python@5fda3b95a4ea91299a34e894583c3862153e4b97 # v7.0.0
9295
with:
9396
python-version: '3.11'
9497
cache: 'pip'

‎.github/workflows/docs.yml‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,20 @@ on:
77
branches: [ main, master, develop ]
88
workflow_dispatch:
99

10+
permissions:
11+
contents: read
12+
1013
jobs:
1114
build-docs:
1215
name: Build Sphinx Documentation
1316
runs-on: ubuntu-latest
1417

1518
steps:
1619
- name: Checkout code
17-
uses: actions/checkout@v4
20+
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
1821

1922
- name: Set up Python
20-
uses: actions/setup-python@v5
23+
uses: actions/setup-python@5fda3b95a4ea91299a34e894583c3862153e4b97 # v7.0.0
2124
with:
2225
python-version: '3.12'
2326
cache: 'pip'
@@ -33,7 +36,7 @@ jobs:
3336
sphinx-build -b html source build/html
3437
3538
- name: Upload documentation artifacts
36-
uses: actions/upload-artifact@v4
39+
uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
3740
if: success()
3841
with:
3942
name: documentation

‎.github/workflows/publish.yml‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ jobs:
1616

1717
steps:
1818
- name: Checkout code
19-
uses: actions/checkout@v4
19+
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
2020

2121
- name: Set up Python
22-
uses: actions/setup-python@v5
22+
uses: actions/setup-python@5fda3b95a4ea91299a34e894583c3862153e4b97 # v7.0.0
2323
with:
2424
python-version: '3.12'
2525
cache: 'pip'

‎src/python_introspect/validation.py‎

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,23 @@
22

33
from __future__ import annotations
44

5-
from collections.abc import Mapping, Sequence, Set
5+
from collections.abc import Callable, Mapping, Sequence
6+
from collections.abc import Set as AbstractSet
67
from dataclasses import fields, is_dataclass, replace
78
from functools import singledispatch
89
from typing import (
910
Annotated,
1011
Any,
1112
ClassVar,
1213
Literal,
14+
TypeVar,
1315
get_args,
1416
get_origin,
1517
get_type_hints,
16-
TypeVar,
1718
)
1819

1920
from annotated_types import Ge, Gt, Interval, Le, Len, Lt, MaxLen, MinLen, Predicate
21+
2022
from .annotation_types import is_union_type
2123

2224

@@ -133,21 +135,21 @@ def validate_annotation_value(
133135
)
134136
if origin is Literal:
135137
choices = get_args(annotation)
136-
if not any(
137-
type(value) is type(choice) and value == choice
138-
for choice in choices
139-
):
138+
if not any(type(value) is type(choice) and value == choice for choice in choices):
140139
raise AnnotationValidationError(f"{path} must be one of {choices!r}.")
141140
return
142141
if origin is tuple:
143142
_validate_tuple(annotation, value, path)
144143
return
145-
if origin in {list, set, frozenset, Sequence, Set}:
144+
if origin in {list, set, frozenset, Sequence, AbstractSet}:
146145
_validate_sequence(annotation, value, path)
147146
return
148147
if origin in {dict, Mapping}:
149148
_validate_mapping(annotation, value, path)
150149
return
150+
if origin is Callable:
151+
_validate_callable(value, path)
152+
return
151153
if origin is ClassVar:
152154
return
153155
if annotation is None or annotation is type(None):
@@ -215,6 +217,11 @@ def _validate_mapping(annotation: object, value: object, path: str) -> None:
215217
validate_annotation_value(item_type, item, path=f"{path}[{key!r}]")
216218

217219

220+
def _validate_callable(value: object, path: str) -> None:
221+
if not callable(value):
222+
raise TypeError(f"{path} must be callable; got {type(value).__name__}.")
223+
224+
218225
def _annotation_label(annotation: object) -> str:
219226
return annotation.__name__ if isinstance(annotation, type) else str(annotation)
220227

‎tests/test_validation.py‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
from collections.abc import Callable
12
from dataclasses import dataclass, make_dataclass
23
from enum import Enum
3-
from typing import Annotated, Literal
4+
from typing import Annotated, Any, Literal
45

56
import pytest
67
from annotated_types import Ge, Gt, Le, MinLen, Predicate
@@ -9,6 +10,7 @@
910
AnnotatedDataclassValidationMixin,
1011
overlay_non_none_dataclass,
1112
validate_annotated_dataclass,
13+
validate_annotation_value,
1214
)
1315

1416

@@ -93,6 +95,19 @@ def __post_init__(self) -> None:
9395
LiteralConfig(value=True)
9496

9597

98+
def test_callable_validation_uses_the_declared_runtime_contract() -> None:
99+
class CallableInstance:
100+
def __call__(self) -> None:
101+
pass
102+
103+
annotation = Callable[..., Any]
104+
validate_annotation_value(annotation, max, path="reducer")
105+
validate_annotation_value(annotation, CallableInstance(), path="reducer")
106+
107+
with pytest.raises(TypeError, match="reducer must be callable"):
108+
validate_annotation_value(annotation, 7, path="reducer")
109+
110+
96111
@dataclass(frozen=True)
97112
class ConnectionSpec:
98113
host: str = "localhost"

0 commit comments

Comments
 (0)