Skip to content

Commit 64eaa9d

Browse files
authored
Merge pull request #88 from smarie/feature/87_typeguard_compat
Fixed `TypeError: check_type() takes 2 positional arguments but 3 were given` with typeguard 3.0.0+
2 parents c08cfb3 + 62f7e0f commit 64eaa9d

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

docs/changelog.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Changelog
22

3+
### 1.7.1 - Compatibility fix for typeguard `3.0.0`
4+
5+
- Fixed `TypeError: check_type() takes 2 positional arguments but 3 were given` triggering erroneous `FieldTypeError`
6+
when `typeguard>=3.0.0` is used. Fixed [#87](https://github.com/smarie/python-pyfields/issues/87)
7+
38
### 1.7.0 - Better support for non-deep-copiable default values in `@autofields`
49

510
- `@autofields` and `@autoclass` now raise an error when a field definition can not be valid, because the default value can not be deep-copied. This will help users detect issues such as [#84](https://github.com/smarie/python-pyfields/issues/84) earlier. Implementation is done through a new `autocheck` option in the `copy_value` factory.

pyfields/typing_utils.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
# License: 3-clause BSD, <https://github.com/smarie/python-pyfields/blob/master/LICENSE>
55
import sys
66

7+
from pkg_resources import get_distribution
8+
79

810
class FieldTypeError(TypeError): # FieldError
911
"""
@@ -45,7 +47,19 @@ def __str__(self):
4547

4648
def _make_assert_is_of_type():
4749
try:
48-
from typeguard import check_type
50+
from typeguard import check_type as ct
51+
from packaging.version import parse as parse_version
52+
53+
# Note: only do this when we are sure that typeguard can be imported, otherwise this is slow
54+
# see https://github.com/smarie/python-getversion/blob/ee495acf6cf06c5e860713edeee396206368e458/getversion/main.py#L84
55+
typeguard_version = get_distribution("typeguard").version
56+
if parse_version(typeguard_version) < parse_version("3.0.0"):
57+
check_type = ct
58+
else:
59+
# Name has disappeared from 3.0.0
60+
def check_type(name, value, typ):
61+
ct(value, typ)
62+
4963
try:
5064
from typing import Union
5165
except ImportError:

0 commit comments

Comments
 (0)