typeguard 4.5.2__tar.gz → 4.6.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {typeguard-4.5.2 → typeguard-4.6.1}/.github/workflows/publish.yml +3 -3
- {typeguard-4.5.2 → typeguard-4.6.1}/.github/workflows/test.yml +3 -3
- {typeguard-4.5.2 → typeguard-4.6.1}/.pre-commit-config.yaml +2 -2
- {typeguard-4.5.2 → typeguard-4.6.1}/PKG-INFO +4 -4
- {typeguard-4.5.2 → typeguard-4.6.1}/docs/conf.py +5 -2
- {typeguard-4.5.2 → typeguard-4.6.1}/docs/contributing.rst +1 -1
- {typeguard-4.5.2 → typeguard-4.6.1}/docs/userguide.rst +17 -3
- {typeguard-4.5.2 → typeguard-4.6.1}/docs/versionhistory.rst +42 -1
- {typeguard-4.5.2 → typeguard-4.6.1}/pyproject.toml +11 -4
- {typeguard-4.5.2 → typeguard-4.6.1}/src/typeguard/_checkers.py +85 -61
- {typeguard-4.5.2 → typeguard-4.6.1}/src/typeguard/_decorators.py +7 -9
- {typeguard-4.5.2 → typeguard-4.6.1}/src/typeguard/_functions.py +1 -5
- {typeguard-4.5.2 → typeguard-4.6.1}/src/typeguard/_importhook.py +63 -22
- {typeguard-4.5.2 → typeguard-4.6.1}/src/typeguard/_memo.py +1 -1
- {typeguard-4.5.2 → typeguard-4.6.1}/src/typeguard/_pytest_plugin.py +15 -1
- {typeguard-4.5.2 → typeguard-4.6.1}/src/typeguard/_suppression.py +1 -7
- {typeguard-4.5.2 → typeguard-4.6.1}/src/typeguard/_transformer.py +24 -24
- {typeguard-4.5.2 → typeguard-4.6.1}/src/typeguard/_utils.py +3 -16
- {typeguard-4.5.2 → typeguard-4.6.1}/src/typeguard.egg-info/PKG-INFO +4 -4
- {typeguard-4.5.2 → typeguard-4.6.1}/src/typeguard.egg-info/SOURCES.txt +2 -0
- typeguard-4.6.1/src/typeguard.egg-info/requires.txt +1 -0
- typeguard-4.6.1/src/typeguard.egg-info/scm_file_list.json +59 -0
- typeguard-4.6.1/src/typeguard.egg-info/scm_version.json +8 -0
- {typeguard-4.5.2 → typeguard-4.6.1}/tests/__init__.py +1 -2
- {typeguard-4.5.2 → typeguard-4.6.1}/tests/conftest.py +1 -3
- {typeguard-4.5.2 → typeguard-4.6.1}/tests/dummymodule.py +6 -20
- typeguard-4.6.1/tests/mypy/negative.py +56 -0
- {typeguard-4.5.2 → typeguard-4.6.1}/tests/test_checkers.py +153 -33
- {typeguard-4.5.2 → typeguard-4.6.1}/tests/test_importhook.py +37 -7
- {typeguard-4.5.2 → typeguard-4.6.1}/tests/test_instrumentation.py +19 -14
- {typeguard-4.5.2 → typeguard-4.6.1}/tests/test_pytest_plugin.py +37 -0
- {typeguard-4.5.2 → typeguard-4.6.1}/tests/test_suppression.py +2 -3
- {typeguard-4.5.2 → typeguard-4.6.1}/tests/test_transformer.py +31 -32
- {typeguard-4.5.2 → typeguard-4.6.1}/tests/test_typechecked.py +62 -6
- typeguard-4.5.2/src/typeguard.egg-info/requires.txt +0 -4
- typeguard-4.5.2/tests/mypy/negative.py +0 -56
- {typeguard-4.5.2 → typeguard-4.6.1}/.github/FUNDING.yml +0 -0
- {typeguard-4.5.2 → typeguard-4.6.1}/.github/ISSUE_TEMPLATE/bug_report.yaml +0 -0
- {typeguard-4.5.2 → typeguard-4.6.1}/.github/ISSUE_TEMPLATE/config.yml +0 -0
- {typeguard-4.5.2 → typeguard-4.6.1}/.github/ISSUE_TEMPLATE/features_request.yaml +0 -0
- {typeguard-4.5.2 → typeguard-4.6.1}/.github/SECURITY.md +0 -0
- {typeguard-4.5.2 → typeguard-4.6.1}/.github/dependabot.yml +0 -0
- {typeguard-4.5.2 → typeguard-4.6.1}/.github/pull_request_template.md +0 -0
- {typeguard-4.5.2 → typeguard-4.6.1}/.gitignore +0 -0
- {typeguard-4.5.2 → typeguard-4.6.1}/.readthedocs.yml +0 -0
- {typeguard-4.5.2 → typeguard-4.6.1}/LICENSE +0 -0
- {typeguard-4.5.2 → typeguard-4.6.1}/README.rst +0 -0
- {typeguard-4.5.2 → typeguard-4.6.1}/docs/api.rst +0 -0
- {typeguard-4.5.2 → typeguard-4.6.1}/docs/extending.rst +0 -0
- {typeguard-4.5.2 → typeguard-4.6.1}/docs/features.rst +0 -0
- {typeguard-4.5.2 → typeguard-4.6.1}/docs/index.rst +0 -0
- {typeguard-4.5.2 → typeguard-4.6.1}/setup.cfg +0 -0
- {typeguard-4.5.2 → typeguard-4.6.1}/src/typeguard/__init__.py +0 -0
- {typeguard-4.5.2 → typeguard-4.6.1}/src/typeguard/_config.py +0 -0
- {typeguard-4.5.2 → typeguard-4.6.1}/src/typeguard/_exceptions.py +0 -0
- {typeguard-4.5.2 → typeguard-4.6.1}/src/typeguard/py.typed +0 -0
- {typeguard-4.5.2 → typeguard-4.6.1}/src/typeguard.egg-info/dependency_links.txt +0 -0
- {typeguard-4.5.2 → typeguard-4.6.1}/src/typeguard.egg-info/entry_points.txt +0 -0
- {typeguard-4.5.2 → typeguard-4.6.1}/src/typeguard.egg-info/top_level.txt +0 -0
- {typeguard-4.5.2 → typeguard-4.6.1}/tests/deferredannos.py +0 -0
- {typeguard-4.5.2 → typeguard-4.6.1}/tests/dummymodule_py312.py +0 -0
- {typeguard-4.5.2 → typeguard-4.6.1}/tests/mypy/positive.py +0 -0
- {typeguard-4.5.2 → typeguard-4.6.1}/tests/mypy/test_type_annotations.py +0 -0
- {typeguard-4.5.2 → typeguard-4.6.1}/tests/test_functions.py +0 -0
- {typeguard-4.5.2 → typeguard-4.6.1}/tests/test_plugins.py +0 -0
- {typeguard-4.5.2 → typeguard-4.6.1}/tests/test_utils.py +0 -0
- {typeguard-4.5.2 → typeguard-4.6.1}/tests/test_warn_on_error.py +0 -0
|
@@ -14,9 +14,9 @@ jobs:
|
|
|
14
14
|
runs-on: ubuntu-latest
|
|
15
15
|
environment: release
|
|
16
16
|
steps:
|
|
17
|
-
- uses: actions/checkout@
|
|
17
|
+
- uses: actions/checkout@v7
|
|
18
18
|
- name: Set up Python
|
|
19
|
-
uses: actions/setup-python@
|
|
19
|
+
uses: actions/setup-python@v7
|
|
20
20
|
with:
|
|
21
21
|
python-version: 3.x
|
|
22
22
|
- name: Install dependencies
|
|
@@ -52,7 +52,7 @@ jobs:
|
|
|
52
52
|
permissions:
|
|
53
53
|
contents: write
|
|
54
54
|
steps:
|
|
55
|
-
- uses: actions/checkout@
|
|
55
|
+
- uses: actions/checkout@v7
|
|
56
56
|
- id: changelog
|
|
57
57
|
uses: agronholm/release-notes@v1
|
|
58
58
|
with:
|
|
@@ -10,12 +10,12 @@ jobs:
|
|
|
10
10
|
strategy:
|
|
11
11
|
fail-fast: false
|
|
12
12
|
matrix:
|
|
13
|
-
python-version: ["3.
|
|
13
|
+
python-version: ["3.10", "3.11", "3.12", "3.13", "3.14", "3.15", pypy-3.11]
|
|
14
14
|
runs-on: ubuntu-latest
|
|
15
15
|
steps:
|
|
16
|
-
- uses: actions/checkout@
|
|
16
|
+
- uses: actions/checkout@v7
|
|
17
17
|
- name: Set up Python ${{ matrix.python-version }}
|
|
18
|
-
uses: actions/setup-python@
|
|
18
|
+
uses: actions/setup-python@v7
|
|
19
19
|
with:
|
|
20
20
|
python-version: ${{ matrix.python-version }}
|
|
21
21
|
allow-prereleases: true
|
|
@@ -14,14 +14,14 @@ repos:
|
|
|
14
14
|
- id: trailing-whitespace
|
|
15
15
|
|
|
16
16
|
- repo: https://github.com/astral-sh/ruff-pre-commit
|
|
17
|
-
rev: v0.
|
|
17
|
+
rev: v0.16.10
|
|
18
18
|
hooks:
|
|
19
19
|
- id: ruff
|
|
20
20
|
args: [--fix, --show-fixes]
|
|
21
21
|
- id: ruff-format
|
|
22
22
|
|
|
23
23
|
- repo: https://github.com/pre-commit/mirrors-mypy
|
|
24
|
-
rev:
|
|
24
|
+
rev: v2.4.0
|
|
25
25
|
hooks:
|
|
26
26
|
- id: mypy
|
|
27
27
|
pass_filenames: false
|
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: typeguard
|
|
3
|
-
Version: 4.
|
|
3
|
+
Version: 4.6.1
|
|
4
4
|
Summary: Run-time type checker for Python
|
|
5
5
|
Author-email: Alex Grönholm <alex.gronholm@nextday.fi>
|
|
6
|
+
Maintainer-email: "Jason R. Coombs" <jaraco@jaraco.com>, André Théo LAURET <andre.lauret@eclipse-technology.eu>
|
|
6
7
|
License-Expression: MIT
|
|
7
8
|
Project-URL: Documentation, https://typeguard.readthedocs.io/en/latest/
|
|
8
9
|
Project-URL: Change log, https://typeguard.readthedocs.io/en/latest/versionhistory.html
|
|
@@ -12,16 +13,15 @@ Classifier: Development Status :: 5 - Production/Stable
|
|
|
12
13
|
Classifier: Intended Audience :: Developers
|
|
13
14
|
Classifier: Programming Language :: Python
|
|
14
15
|
Classifier: Programming Language :: Python :: 3
|
|
15
|
-
Classifier: Programming Language :: Python :: 3.9
|
|
16
16
|
Classifier: Programming Language :: Python :: 3.10
|
|
17
17
|
Classifier: Programming Language :: Python :: 3.11
|
|
18
18
|
Classifier: Programming Language :: Python :: 3.12
|
|
19
19
|
Classifier: Programming Language :: Python :: 3.13
|
|
20
20
|
Classifier: Programming Language :: Python :: 3.14
|
|
21
|
-
|
|
21
|
+
Classifier: Programming Language :: Python :: 3.15
|
|
22
|
+
Requires-Python: >=3.10
|
|
22
23
|
Description-Content-Type: text/x-rst
|
|
23
24
|
License-File: LICENSE
|
|
24
|
-
Requires-Dist: importlib_metadata>=3.6; python_version < "3.10"
|
|
25
25
|
Requires-Dist: typing_extensions>=4.14.0
|
|
26
26
|
Dynamic: license-file
|
|
27
27
|
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
#!/usr/bin/env python3
|
|
2
1
|
from importlib.metadata import version as get_version
|
|
3
2
|
|
|
4
3
|
from packaging.version import parse
|
|
@@ -35,4 +34,8 @@ todo_include_todos = False
|
|
|
35
34
|
html_theme = "sphinx_rtd_theme"
|
|
36
35
|
htmlhelp_basename = "typeguarddoc"
|
|
37
36
|
|
|
38
|
-
intersphinx_mapping = {
|
|
37
|
+
intersphinx_mapping = {
|
|
38
|
+
# Keep references to removed APIs and pre-3.14 typing object kinds resolvable.
|
|
39
|
+
"legacy-python": ("https://docs.python.org/3.13/", None),
|
|
40
|
+
"python": ("https://docs.python.org/3/", None),
|
|
41
|
+
}
|
|
@@ -61,7 +61,7 @@ To get your changes merged to the main codebase, you need a Github account.
|
|
|
61
61
|
#. Fork the repository (if you don't have your own fork of it yet) by navigating to the
|
|
62
62
|
`main Typeguard repository`_ and clicking on "Fork" near the top right corner.
|
|
63
63
|
#. Clone the forked repository to your local machine with
|
|
64
|
-
``git clone git@github.com
|
|
64
|
+
``git clone git@github.com:yourusername/typeguard``.
|
|
65
65
|
#. Create a branch for your pull request, like ``git checkout -b myfixname``
|
|
66
66
|
#. Make the desired changes to the code base.
|
|
67
67
|
#. Commit your changes locally. If your changes close an existing issue, add the text
|
|
@@ -6,8 +6,8 @@ User guide
|
|
|
6
6
|
Checking types directly
|
|
7
7
|
-----------------------
|
|
8
8
|
|
|
9
|
-
The most
|
|
10
|
-
:func:`.check_type`. It can be used as
|
|
9
|
+
The most straightforward way to do type checking with Typeguard is with
|
|
10
|
+
:func:`.check_type`. It can be used as a beefed-up version of :func:`isinstance` that
|
|
11
11
|
also supports checking against annotations in the :mod:`typing` module::
|
|
12
12
|
|
|
13
13
|
from typeguard import check_type
|
|
@@ -131,6 +131,13 @@ list of package names)::
|
|
|
131
131
|
install_import_hook('myapp')
|
|
132
132
|
from myapp import some_module # import only AFTER installing the hook, or it won't take effect
|
|
133
133
|
|
|
134
|
+
You can also exclude specific packages or modules from instrumentation using the
|
|
135
|
+
``ignore_packages`` parameter::
|
|
136
|
+
|
|
137
|
+
from typeguard import install_import_hook
|
|
138
|
+
|
|
139
|
+
install_import_hook('myapp', ignore_packages=['myapp.tests'])
|
|
140
|
+
|
|
134
141
|
If you wish, you can uninstall the import hook::
|
|
135
142
|
|
|
136
143
|
manager = install_import_hook('myapp')
|
|
@@ -184,7 +191,12 @@ Typeguard comes with a plugin for pytest (v7.0 or newer) that installs the impor
|
|
|
184
191
|
|
|
185
192
|
pytest --typeguard-packages=foo.bar,xyz
|
|
186
193
|
|
|
187
|
-
|
|
194
|
+
You can also exclude specific packages or modules from type checking using the
|
|
195
|
+
``--typeguard-packages-ignore`` option::
|
|
196
|
+
|
|
197
|
+
pytest --typeguard-packages=foo.bar,xyz --typeguard-packages-ignore=foo.bar.tests
|
|
198
|
+
|
|
199
|
+
It is also possible to set options for the pytest plugin using pytest's own
|
|
188
200
|
configuration. For example, here's how you might specify several options in
|
|
189
201
|
``pyproject.toml``:
|
|
190
202
|
|
|
@@ -194,6 +206,8 @@ configuration. For example, here's how you might specify several options in
|
|
|
194
206
|
typeguard-packages = """
|
|
195
207
|
foo.bar
|
|
196
208
|
xyz"""
|
|
209
|
+
typeguard-packages-ignore = """
|
|
210
|
+
foo.bar.tests"""
|
|
197
211
|
typeguard-debug-instrumentation = true
|
|
198
212
|
typeguard-typecheck-fail-callback = "mypackage:failcallback"
|
|
199
213
|
typeguard-forward-ref-policy = "ERROR"
|
|
@@ -4,6 +4,47 @@ Version history
|
|
|
4
4
|
This library adheres to
|
|
5
5
|
`Semantic Versioning 2.0 <https://semver.org/#semantic-versioning-200>`_.
|
|
6
6
|
|
|
7
|
+
**4.6.1** (2026-11-19)
|
|
8
|
+
|
|
9
|
+
- Fixed documentation builds failing on references to removed typing APIs and older
|
|
10
|
+
typing object kinds by adding a legacy Python documentation inventory
|
|
11
|
+
(`#612 <https://github.com/agronholm/typeguard/issues/612>`_; PR by @parijat1222q)
|
|
12
|
+
|
|
13
|
+
- Fixed ``ReadOnly`` (:pep:`705`) qualifiers on ``TypedDict`` items being left
|
|
14
|
+
unwrapped, which caused the item's value type to skip type checking entirely
|
|
15
|
+
(`#571 <https://github.com/agronholm/typeguard/pull/571>`_; PR by @jaideeppyne)
|
|
16
|
+
|
|
17
|
+
**4.6.0** (2026-07-26)
|
|
18
|
+
|
|
19
|
+
- Added support for type checking against the PEP 661 ``sentinel`` type (built-in on
|
|
20
|
+
Python 3.15 and available on ``typing_extensions`` for earlier Pythons)
|
|
21
|
+
- Added the ``typeguard-packages-ignore`` option (``--typeguard-packages-ignore`` on the
|
|
22
|
+
command line) to the pytest plugin to exclude specific packages from type checking
|
|
23
|
+
(`#523 <https://github.com/agronholm/typeguard/issues/523>`_)
|
|
24
|
+
- Added recognition of wildcard imports from ``typing`` and ``typing_extensions`` in the
|
|
25
|
+
import hook/instrumenter
|
|
26
|
+
(`#489 <https://github.com/agronholm/typeguard/issues/489>`_; PR by @ShipitAndPray)
|
|
27
|
+
- Fixed compatibility with Python 3.15
|
|
28
|
+
(`#554 <https://github.com/agronholm/typeguard/pull/554>`_; PR by @hrnciar)
|
|
29
|
+
- Fixed an assignment expression against an annotated name (``x: int``, then
|
|
30
|
+
``if (x := ...)``) being instrumented as an unpacking target, which raised
|
|
31
|
+
``TypeError`` for a non-iterable value and silently replaced an iterable value with
|
|
32
|
+
``list(value)``
|
|
33
|
+
(`#557 <https://github.com/agronholm/typeguard/issues/557>`_)
|
|
34
|
+
- Fixed false positive when checking a class against ``type[SomeProtocol]`` where the
|
|
35
|
+
protocol declares non-``ClassVar`` (instance) attributes; only ``ClassVar`` members
|
|
36
|
+
are now required on the class itself
|
|
37
|
+
(`#499 <https://github.com/agronholm/typeguard/issues/499>`_)
|
|
38
|
+
- Fixed ``Literal`` checks depending on the order of the arguments, where a ``bool``
|
|
39
|
+
would not match if an ``==`` equal ``int`` came first (``Literal[1, True]`` rejected
|
|
40
|
+
``True``, but ``Literal[True, 1]`` accepted it)
|
|
41
|
+
(`#566 <https://github.com/agronholm/typeguard/pull/566>`_; PR by @uttam12331)
|
|
42
|
+
- Fixed the import hook/instrumenter erasing the subscript of an empty tuple annotation
|
|
43
|
+
(``tuple[()]``) down to a bare ``tuple``, causing any tuple (including non-empty ones)
|
|
44
|
+
to pass the check
|
|
45
|
+
(`#565 <https://github.com/agronholm/typeguard/pull/565>`_; PR by @chuenchen309)
|
|
46
|
+
- Dropped support for Python 3.9
|
|
47
|
+
|
|
7
48
|
**4.5.2** (2026-05-14)
|
|
8
49
|
|
|
9
50
|
- Fixed ``IndexError`` raised from ``check_signature_compatible`` when the subject
|
|
@@ -589,7 +630,7 @@ This library adheres to
|
|
|
589
630
|
|
|
590
631
|
**1.2.2** (2016-08-23)
|
|
591
632
|
|
|
592
|
-
- Fixed checking of
|
|
633
|
+
- Fixed checking of homogeneous Tuple declarations (``Tuple[bool, ...]``)
|
|
593
634
|
|
|
594
635
|
**1.2.1** (2016-06-29)
|
|
595
636
|
|
|
@@ -10,22 +10,25 @@ name = "typeguard"
|
|
|
10
10
|
description = "Run-time type checker for Python"
|
|
11
11
|
readme = "README.rst"
|
|
12
12
|
authors = [{name = "Alex Grönholm", email = "alex.gronholm@nextday.fi"}]
|
|
13
|
+
maintainers = [
|
|
14
|
+
{name = "Jason R. Coombs", email = "jaraco@jaraco.com"},
|
|
15
|
+
{name = "André Théo LAURET", email = "andre.lauret@eclipse-technology.eu"},
|
|
16
|
+
]
|
|
13
17
|
license = "MIT"
|
|
14
18
|
classifiers = [
|
|
15
19
|
"Development Status :: 5 - Production/Stable",
|
|
16
20
|
"Intended Audience :: Developers",
|
|
17
21
|
"Programming Language :: Python",
|
|
18
22
|
"Programming Language :: Python :: 3",
|
|
19
|
-
"Programming Language :: Python :: 3.9",
|
|
20
23
|
"Programming Language :: Python :: 3.10",
|
|
21
24
|
"Programming Language :: Python :: 3.11",
|
|
22
25
|
"Programming Language :: Python :: 3.12",
|
|
23
26
|
"Programming Language :: Python :: 3.13",
|
|
24
27
|
"Programming Language :: Python :: 3.14",
|
|
28
|
+
"Programming Language :: Python :: 3.15",
|
|
25
29
|
]
|
|
26
|
-
requires-python = ">= 3.
|
|
30
|
+
requires-python = ">= 3.10"
|
|
27
31
|
dependencies = [
|
|
28
|
-
"importlib_metadata >= 3.6; python_version < '3.10'",
|
|
29
32
|
"typing_extensions >= 4.14.0",
|
|
30
33
|
]
|
|
31
34
|
dynamic = ["version"]
|
|
@@ -81,6 +84,9 @@ src = ["src"]
|
|
|
81
84
|
[tool.ruff.per-file-target-version]
|
|
82
85
|
"tests/dummymodule_py312.py" = "py312"
|
|
83
86
|
|
|
87
|
+
[tool.ruff.format]
|
|
88
|
+
exclude = ["tests/mypy/negative.py"]
|
|
89
|
+
|
|
84
90
|
[tool.ruff.lint]
|
|
85
91
|
extend-select = [
|
|
86
92
|
"B0", # flake8-bugbear
|
|
@@ -93,6 +99,7 @@ ignore = [
|
|
|
93
99
|
"S307",
|
|
94
100
|
"B008",
|
|
95
101
|
"UP006",
|
|
102
|
+
"UP007",
|
|
96
103
|
"UP035",
|
|
97
104
|
]
|
|
98
105
|
|
|
@@ -103,7 +110,7 @@ pretty = true
|
|
|
103
110
|
files = ["src"]
|
|
104
111
|
|
|
105
112
|
[tool.tox]
|
|
106
|
-
env_list = ["
|
|
113
|
+
env_list = ["py310", "py311", "py312", "py313", "py314", "py315"]
|
|
107
114
|
skip_missing_interpreters = true
|
|
108
115
|
requires = ["tox >= 4.22"]
|
|
109
116
|
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
from __future__ import annotations
|
|
2
2
|
|
|
3
|
+
import builtins
|
|
3
4
|
import collections.abc
|
|
4
5
|
import inspect
|
|
5
6
|
import sys
|
|
@@ -8,10 +9,11 @@ import typing
|
|
|
8
9
|
import warnings
|
|
9
10
|
from collections.abc import Mapping, MutableMapping, Sequence
|
|
10
11
|
from enum import Enum
|
|
11
|
-
from inspect import Parameter, isclass
|
|
12
|
+
from inspect import Parameter, isclass
|
|
12
13
|
from io import BufferedIOBase, IOBase, RawIOBase, TextIOBase
|
|
13
14
|
from itertools import zip_longest
|
|
14
15
|
from textwrap import indent
|
|
16
|
+
from types import UnionType
|
|
15
17
|
from typing import (
|
|
16
18
|
IO,
|
|
17
19
|
AbstractSet,
|
|
@@ -23,11 +25,11 @@ from typing import (
|
|
|
23
25
|
ForwardRef,
|
|
24
26
|
List,
|
|
25
27
|
NewType,
|
|
26
|
-
Optional,
|
|
27
28
|
Set,
|
|
28
29
|
TextIO,
|
|
29
30
|
Tuple,
|
|
30
31
|
Type,
|
|
32
|
+
TypeGuard,
|
|
31
33
|
TypeVar,
|
|
32
34
|
Union,
|
|
33
35
|
)
|
|
@@ -51,6 +53,11 @@ if sys.version_info >= (3, 15):
|
|
|
51
53
|
else:
|
|
52
54
|
from typing_extensions import NoExtraItems
|
|
53
55
|
|
|
56
|
+
if sys.version_info >= (3, 13):
|
|
57
|
+
from typing import ReadOnly
|
|
58
|
+
else:
|
|
59
|
+
from typing_extensions import ReadOnly
|
|
60
|
+
|
|
54
61
|
if sys.version_info >= (3, 11):
|
|
55
62
|
from typing import (
|
|
56
63
|
NotRequired,
|
|
@@ -71,18 +78,14 @@ else:
|
|
|
71
78
|
get_origin,
|
|
72
79
|
)
|
|
73
80
|
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
from typing import ParamSpec
|
|
77
|
-
else:
|
|
78
|
-
from importlib_metadata import entry_points
|
|
79
|
-
from typing_extensions import ParamSpec
|
|
81
|
+
from importlib.metadata import entry_points
|
|
82
|
+
from typing import ParamSpec
|
|
80
83
|
|
|
81
84
|
TypeCheckerCallable: TypeAlias = Callable[
|
|
82
85
|
[Any, Any, Tuple[Any, ...], TypeCheckMemo], Any
|
|
83
86
|
]
|
|
84
87
|
TypeCheckLookupCallback: TypeAlias = Callable[
|
|
85
|
-
[Any, Tuple[Any, ...], Tuple[Any, ...]],
|
|
88
|
+
[Any, Tuple[Any, ...], Tuple[Any, ...]], TypeCheckerCallable | None
|
|
86
89
|
]
|
|
87
90
|
|
|
88
91
|
checker_lookup_functions: list[TypeCheckLookupCallback] = []
|
|
@@ -95,6 +98,11 @@ generic_alias_types: tuple[type, ...] = (
|
|
|
95
98
|
# Sentinel
|
|
96
99
|
_missing = object()
|
|
97
100
|
|
|
101
|
+
# PEP 661 sentinel type instances (typing_extensions.Sentinel, built-in sentinel in 3.15+)
|
|
102
|
+
_sentinel_types: tuple[type, ...] = (typing_extensions.Sentinel,)
|
|
103
|
+
if sys.version_info >= (3, 15):
|
|
104
|
+
_sentinel_types += (builtins.sentinel,)
|
|
105
|
+
|
|
98
106
|
# Lifted from mypy.sharedparse
|
|
99
107
|
BINARY_MAGIC_METHODS = {
|
|
100
108
|
"__add__",
|
|
@@ -215,9 +223,8 @@ def check_mapping(
|
|
|
215
223
|
args: tuple[Any, ...],
|
|
216
224
|
memo: TypeCheckMemo,
|
|
217
225
|
) -> None:
|
|
218
|
-
if origin_type is Dict or origin_type is dict:
|
|
219
|
-
|
|
220
|
-
raise TypeCheckError("is not a dict")
|
|
226
|
+
if (origin_type is Dict or origin_type is dict) and not isinstance(value, dict):
|
|
227
|
+
raise TypeCheckError("is not a dict")
|
|
221
228
|
if origin_type is MutableMapping or origin_type is collections.abc.MutableMapping:
|
|
222
229
|
if not isinstance(value, collections.abc.MutableMapping):
|
|
223
230
|
raise TypeCheckError("is not a mutable mapping")
|
|
@@ -279,12 +286,21 @@ def check_typed_dict(
|
|
|
279
286
|
if isinstance(annotation, ForwardRef):
|
|
280
287
|
annotation = evaluate_forwardref(annotation, memo)
|
|
281
288
|
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
289
|
+
# Strip the NotRequired/Required/ReadOnly qualifiers (in any nesting
|
|
290
|
+
# order) so the underlying type is actually checked. ReadOnly (PEP 705)
|
|
291
|
+
# was previously left wrapped, which silently skipped type checking.
|
|
292
|
+
while True:
|
|
293
|
+
origin = get_origin(annotation)
|
|
294
|
+
if origin is NotRequired:
|
|
295
|
+
required_keys.discard(key)
|
|
296
|
+
annotation = get_args(annotation)[0]
|
|
297
|
+
elif origin is Required:
|
|
298
|
+
required_keys.add(key)
|
|
299
|
+
annotation = get_args(annotation)[0]
|
|
300
|
+
elif origin is ReadOnly:
|
|
301
|
+
annotation = get_args(annotation)[0]
|
|
302
|
+
else:
|
|
303
|
+
break
|
|
288
304
|
|
|
289
305
|
type_hints[key] = annotation
|
|
290
306
|
|
|
@@ -451,7 +467,7 @@ def check_uniontype(
|
|
|
451
467
|
memo: TypeCheckMemo,
|
|
452
468
|
) -> None:
|
|
453
469
|
if not args:
|
|
454
|
-
return check_instance(value,
|
|
470
|
+
return check_instance(value, UnionType, (), memo)
|
|
455
471
|
|
|
456
472
|
errors: dict[str, TypeCheckError] = {}
|
|
457
473
|
try:
|
|
@@ -511,13 +527,12 @@ def check_class(
|
|
|
511
527
|
return
|
|
512
528
|
except TypeCheckError as exc:
|
|
513
529
|
errors[get_type_name(arg)] = exc
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
)
|
|
530
|
+
formatted_errors = indent(
|
|
531
|
+
"\n".join(f"{key}: {error}" for key, error in errors.items()), " "
|
|
532
|
+
)
|
|
533
|
+
raise TypeCheckError(
|
|
534
|
+
f"did not match any element in the union:\n{formatted_errors}"
|
|
535
|
+
)
|
|
521
536
|
finally:
|
|
522
537
|
del errors # avoid creating ref cycle
|
|
523
538
|
else:
|
|
@@ -608,12 +623,10 @@ def check_literal(
|
|
|
608
623
|
return tuple(retval)
|
|
609
624
|
|
|
610
625
|
final_args = tuple(get_literal_args(args))
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
else:
|
|
616
|
-
if type(final_args[index]) is type(value):
|
|
626
|
+
for arg in final_args:
|
|
627
|
+
# the type check has to come first, as bool is a subclass of int
|
|
628
|
+
# (so 1 == True) and it short-circuits any non-boolean __eq__ result
|
|
629
|
+
if type(arg) is type(value) and arg == value:
|
|
617
630
|
return
|
|
618
631
|
|
|
619
632
|
formatted_args = ", ".join(repr(arg) for arg in final_args)
|
|
@@ -648,6 +661,16 @@ def check_none(
|
|
|
648
661
|
raise TypeCheckError("is not None")
|
|
649
662
|
|
|
650
663
|
|
|
664
|
+
def check_sentinel(
|
|
665
|
+
value: Any,
|
|
666
|
+
origin_type: Any,
|
|
667
|
+
args: tuple[Any, ...],
|
|
668
|
+
memo: TypeCheckMemo,
|
|
669
|
+
) -> None:
|
|
670
|
+
if value is not origin_type:
|
|
671
|
+
raise TypeCheckError(f"is not {origin_type!r}")
|
|
672
|
+
|
|
673
|
+
|
|
651
674
|
def check_number(
|
|
652
675
|
value: Any,
|
|
653
676
|
origin_type: Any,
|
|
@@ -806,29 +829,31 @@ def check_signature_compatible(subject: type, protocol: type, attrname: str) ->
|
|
|
806
829
|
for param in subject_sig.parameters.values()
|
|
807
830
|
if param.kind is Parameter.KEYWORD_ONLY
|
|
808
831
|
}
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
832
|
+
# Check that the signature has at least the required keyword-only arguments, and
|
|
833
|
+
# no extra mandatory keyword-only arguments
|
|
834
|
+
if not subject_has_varkwargs and (
|
|
835
|
+
missing_kwonlyargs := [
|
|
813
836
|
param.name
|
|
814
837
|
for param in protocol_kwonlyargs.values()
|
|
815
838
|
if param.name not in subject_kwonlyargs
|
|
816
|
-
]
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
)
|
|
839
|
+
]
|
|
840
|
+
):
|
|
841
|
+
raise TypeCheckError(
|
|
842
|
+
"is missing keyword-only arguments: " + ", ".join(missing_kwonlyargs)
|
|
843
|
+
)
|
|
820
844
|
|
|
821
|
-
if not protocol_has_varkwargs
|
|
822
|
-
|
|
845
|
+
if not protocol_has_varkwargs and (
|
|
846
|
+
extra_kwonlyargs := [
|
|
823
847
|
param.name
|
|
824
848
|
for param in subject_kwonlyargs.values()
|
|
825
849
|
if param.default is Parameter.empty
|
|
826
850
|
and param.name not in protocol_kwonlyargs
|
|
827
|
-
]
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
)
|
|
851
|
+
]
|
|
852
|
+
):
|
|
853
|
+
raise TypeCheckError(
|
|
854
|
+
"has mandatory keyword-only arguments not present in the protocol: "
|
|
855
|
+
+ ", ".join(extra_kwonlyargs)
|
|
856
|
+
)
|
|
832
857
|
|
|
833
858
|
|
|
834
859
|
def check_protocol(
|
|
@@ -838,8 +863,15 @@ def check_protocol(
|
|
|
838
863
|
memo: TypeCheckMemo,
|
|
839
864
|
) -> None:
|
|
840
865
|
origin_annotations = typing.get_type_hints(origin_type)
|
|
866
|
+
checking_class = isclass(value)
|
|
841
867
|
for attrname in sorted(typing_extensions.get_protocol_members(origin_type)):
|
|
842
868
|
if (annotation := origin_annotations.get(attrname)) is not None:
|
|
869
|
+
if checking_class and typing.get_origin(annotation) is not typing.ClassVar:
|
|
870
|
+
# a non-ClassVar annotation is an instance attribute (PEP 544) —
|
|
871
|
+
# a class object isn't expected to have it set, and type
|
|
872
|
+
# checkers accept that, so don't flag it as missing.
|
|
873
|
+
continue
|
|
874
|
+
|
|
843
875
|
try:
|
|
844
876
|
subject_member = getattr(value, attrname)
|
|
845
877
|
except AttributeError:
|
|
@@ -992,7 +1024,7 @@ def check_type_internal(
|
|
|
992
1024
|
if isclass(origin_type):
|
|
993
1025
|
if not isinstance(value, origin_type):
|
|
994
1026
|
raise TypeCheckError(f"is not an instance of {qualified_name(origin_type)}")
|
|
995
|
-
elif type(origin_type) is str:
|
|
1027
|
+
elif type(origin_type) is str:
|
|
996
1028
|
warnings.warn(
|
|
997
1029
|
f"Skipping type check against {origin_type!r}; this looks like a "
|
|
998
1030
|
f"string-form forward reference imported from another module",
|
|
@@ -1034,7 +1066,9 @@ origin_type_checkers: dict[
|
|
|
1034
1066
|
Tuple: check_tuple,
|
|
1035
1067
|
type: check_class,
|
|
1036
1068
|
Type: check_class,
|
|
1069
|
+
TypeGuard: check_typeguard,
|
|
1037
1070
|
Union: check_union,
|
|
1071
|
+
UnionType: check_uniontype,
|
|
1038
1072
|
# On some versions of Python, these may simply be re-exports from "typing",
|
|
1039
1073
|
# but exactly which Python versions is subject to change.
|
|
1040
1074
|
# It's best to err on the safe side and just always specify these.
|
|
@@ -1043,9 +1077,6 @@ origin_type_checkers: dict[
|
|
|
1043
1077
|
typing_extensions.Self: check_self,
|
|
1044
1078
|
typing_extensions.TypeGuard: check_typeguard,
|
|
1045
1079
|
}
|
|
1046
|
-
if sys.version_info >= (3, 10):
|
|
1047
|
-
origin_type_checkers[types.UnionType] = check_uniontype
|
|
1048
|
-
origin_type_checkers[typing.TypeGuard] = check_typeguard
|
|
1049
1080
|
|
|
1050
1081
|
if sys.version_info >= (3, 11):
|
|
1051
1082
|
origin_type_checkers.update(
|
|
@@ -1079,16 +1110,9 @@ def builtin_checker_lookup(
|
|
|
1079
1110
|
elif isinstance(origin_type, TypeVar):
|
|
1080
1111
|
return check_typevar
|
|
1081
1112
|
elif origin_type.__class__ is NewType:
|
|
1082
|
-
# typing.NewType on Python 3.10+
|
|
1083
|
-
return check_newtype
|
|
1084
|
-
elif (
|
|
1085
|
-
isfunction(origin_type)
|
|
1086
|
-
and getattr(origin_type, "__module__", None) == "typing"
|
|
1087
|
-
and getattr(origin_type, "__qualname__", "").startswith("NewType.")
|
|
1088
|
-
and hasattr(origin_type, "__supertype__")
|
|
1089
|
-
):
|
|
1090
|
-
# typing.NewType on Python 3.9
|
|
1091
1113
|
return check_newtype
|
|
1114
|
+
elif isinstance(origin_type, _sentinel_types):
|
|
1115
|
+
return check_sentinel
|
|
1092
1116
|
|
|
1093
1117
|
return None
|
|
1094
1118
|
|
|
@@ -1110,7 +1134,7 @@ def load_plugins() -> None:
|
|
|
1110
1134
|
for ep in entry_points(group="typeguard.checker_lookup"):
|
|
1111
1135
|
try:
|
|
1112
1136
|
plugin = ep.load()
|
|
1113
|
-
except Exception as exc:
|
|
1137
|
+
except Exception as exc: # noqa: BLE001
|
|
1114
1138
|
warnings.warn(
|
|
1115
1139
|
f"Failed to load plugin {ep.name!r}: {qualified_name(exc)}: {exc}",
|
|
1116
1140
|
stacklevel=2,
|
|
@@ -6,7 +6,7 @@ import sys
|
|
|
6
6
|
from collections.abc import Sequence
|
|
7
7
|
from functools import partial
|
|
8
8
|
from inspect import isclass, isfunction
|
|
9
|
-
from types import CodeType, FrameType, FunctionType
|
|
9
|
+
from types import CellType, CodeType, FrameType, FunctionType
|
|
10
10
|
from typing import TYPE_CHECKING, Any, Callable, ForwardRef, TypeVar, cast, overload
|
|
11
11
|
from warnings import warn
|
|
12
12
|
|
|
@@ -19,7 +19,6 @@ from ._utils import Unset, function_name, get_stacklevel, is_method_of, unset
|
|
|
19
19
|
T_CallableOrType = TypeVar("T_CallableOrType", bound=Callable[..., Any])
|
|
20
20
|
|
|
21
21
|
if TYPE_CHECKING:
|
|
22
|
-
from typeshed.stdlib.types import _Cell
|
|
23
22
|
|
|
24
23
|
def typeguard_ignore(arg: T_CallableOrType) -> T_CallableOrType:
|
|
25
24
|
"""This decorator is a noop during static type-checking."""
|
|
@@ -29,7 +28,7 @@ else:
|
|
|
29
28
|
from typing import no_type_check as typeguard_ignore # noqa: F401
|
|
30
29
|
|
|
31
30
|
|
|
32
|
-
def make_cell(value: object) ->
|
|
31
|
+
def make_cell(value: object) -> CellType:
|
|
33
32
|
return (lambda: value).__closure__[0] # type: ignore[index]
|
|
34
33
|
|
|
35
34
|
|
|
@@ -82,7 +81,7 @@ def instrument(f: T_CallableOrType) -> FunctionType | str:
|
|
|
82
81
|
if not new_code:
|
|
83
82
|
return "cannot find the target function in the AST"
|
|
84
83
|
|
|
85
|
-
if global_config.debug_instrumentation
|
|
84
|
+
if global_config.debug_instrumentation:
|
|
86
85
|
# Find the matching AST node, then unparse it to source and print to stdout
|
|
87
86
|
print(
|
|
88
87
|
f"Source code of {f.__qualname__}() after instrumentation:"
|
|
@@ -101,7 +100,7 @@ def instrument(f: T_CallableOrType) -> FunctionType | str:
|
|
|
101
100
|
frame = cast(FrameType, inspect.currentframe())
|
|
102
101
|
frame = cast(FrameType, frame.f_back)
|
|
103
102
|
frame_locals = cast(FrameType, frame.f_back).f_locals
|
|
104
|
-
cells: list[
|
|
103
|
+
cells: list[CellType] = []
|
|
105
104
|
for key in new_code.co_freevars:
|
|
106
105
|
if key in instrumentor.names_used_in_annotations:
|
|
107
106
|
# Find the value and make a new cell from it
|
|
@@ -207,7 +206,7 @@ def typechecked(
|
|
|
207
206
|
wrapper = attr.__class__(retval)
|
|
208
207
|
setattr(target, key, wrapper)
|
|
209
208
|
elif isinstance(attr, property):
|
|
210
|
-
kwargs: dict[str, Any] =
|
|
209
|
+
kwargs: dict[str, Any] = {"doc": attr.__doc__}
|
|
211
210
|
for name in ("fset", "fget", "fdel"):
|
|
212
211
|
property_func = kwargs[name] = getattr(attr, name)
|
|
213
212
|
if is_method_of(property_func, target):
|
|
@@ -220,9 +219,8 @@ def typechecked(
|
|
|
220
219
|
return target
|
|
221
220
|
|
|
222
221
|
# Find either the first Python wrapper or the actual function
|
|
223
|
-
wrapper_class:
|
|
224
|
-
|
|
225
|
-
) = None
|
|
222
|
+
wrapper_class: type[classmethod[Any, Any, Any] | staticmethod[Any, Any]] | None
|
|
223
|
+
wrapper_class = None
|
|
226
224
|
if isinstance(target, (classmethod, staticmethod)):
|
|
227
225
|
wrapper_class = target.__class__
|
|
228
226
|
target = target.__func__ # type: ignore[assignment]
|
|
@@ -136,11 +136,7 @@ def check_argument_types() -> Literal[True]:
|
|
|
136
136
|
func = find_function(frame)
|
|
137
137
|
f_locals = frame.f_locals
|
|
138
138
|
memo = TypeCheckMemo(frame.f_globals, frame.f_locals)
|
|
139
|
-
|
|
140
|
-
sig = signature(func, globals=frame.f_globals, locals=frame.f_locals)
|
|
141
|
-
else:
|
|
142
|
-
sig = signature(func)
|
|
143
|
-
|
|
139
|
+
sig = signature(func, globals=frame.f_globals, locals=frame.f_locals)
|
|
144
140
|
arguments = {}
|
|
145
141
|
for param in sig.parameters.values():
|
|
146
142
|
if param.annotation is Parameter.empty or param.annotation is Any:
|