Skip to content

Commit

Permalink
Upgrade typeguard from v2 to v4
Browse files Browse the repository at this point in the history
  • Loading branch information
faph committed Oct 3, 2023
1 parent bae7cfb commit 15ec1a7
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 11 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ dependencies = [
"avro~=1.10",
"memoization~=0.4",
"orjson~=3.5",
"typeguard~=2.12",
"typeguard~=4.0",
]


Expand Down
6 changes: 3 additions & 3 deletions src/py_avro_schema/_schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -592,10 +592,10 @@ def sort_item_schemas(self, default_value: Any) -> None:
default_index = -1
for i, item_schema in enumerate(self.item_schemas):
try:
typeguard.check_type("default", default_value, item_schema.py_type)
typeguard.check_type(default_value, item_schema.py_type)
default_index = i
break
except TypeError:
except typeguard.TypeCheckError:
continue
if default_index > 0:
default_item_schema = self.item_schemas.pop(default_index)
Expand Down Expand Up @@ -751,7 +751,7 @@ def __init__(
if self.default != dataclasses.MISSING:
if isinstance(self.schema, UnionSchema):
self.schema.sort_item_schemas(self.default)
typeguard.check_type(f"field {self}", self.default, self.py_type)
typeguard.check_type(self.default, self.py_type)
else:
if Option.DEFAULTS_MANDATORY in self.options:
raise TypeError(f"Default value for field {self} is missing")
Expand Down
5 changes: 3 additions & 2 deletions tests/test_dataclass.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from typing import Dict, List, Optional

import pytest
import typeguard

import py_avro_schema as pas
from py_avro_schema._testing import assert_schema
Expand Down Expand Up @@ -94,7 +95,7 @@ def test_string_field_default_wrong_type():
class PyType:
field_a: str = None # That's not valid, because field type is str

with pytest.raises(TypeError, match="type of field field_a must be str; got NoneType instead"):
with pytest.raises(typeguard.TypeCheckError, match="None is not an instance of str"):
assert_schema(PyType, {})


Expand Down Expand Up @@ -168,7 +169,7 @@ def test_list_string_field_default_wrong_type():
class PyType:
field_a: List[str] = dataclasses.field(default_factory=lambda: [1])

with pytest.raises(TypeError, match=re.escape("type of field field_a[0] must be str; got int instead")):
with pytest.raises(typeguard.TypeCheckError, match="item 0 of list is not an instance of str"):
assert_schema(PyType, {})


Expand Down
6 changes: 3 additions & 3 deletions tests/test_pydantic.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
# specific language governing permissions and limitations under the License.

import re
import uuid
from typing import List, Optional

import pydantic
import pytest
import typeguard

from py_avro_schema._testing import assert_schema

Expand Down Expand Up @@ -58,7 +58,7 @@ def test_string_field_default_wrong_type():
class PyType(pydantic.BaseModel):
field_a: str = 1 # That's not valid, because field type is str

with pytest.raises(TypeError, match="type of field field_a must be str; got int instead"):
with pytest.raises(typeguard.TypeCheckError, match="int is not an instance of str"):
assert_schema(PyType, {})


Expand Down Expand Up @@ -128,7 +128,7 @@ def test_list_string_field_default_wrong_type():
class PyType(pydantic.BaseModel):
field_a: List[str] = [1] # Pydantic allows mutable defaults like this

with pytest.raises(TypeError, match=re.escape("type of field field_a[0] must be str; got int instead")):
with pytest.raises(typeguard.TypeCheckError, match="item 0 of list is not an instance of str"):
assert_schema(PyType, {})


Expand Down
5 changes: 3 additions & 2 deletions tests/test_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
# specific language governing permissions and limitations under the License.

import decimal
import re

import pytest
import typeguard
Expand All @@ -25,7 +26,7 @@ def test_decimal_type():

def test_instance_check():
py_type = DecimalType[4, 2]
typeguard.check_type("py_type", decimal.Decimal("1.23"), py_type)
typeguard.check_type(decimal.Decimal("1.23"), py_type)


def test_zero_precision():
Expand All @@ -49,5 +50,5 @@ def test_precision_lt_scale():


def test_bad_indexing():
with pytest.raises(TypeError, match='type of argument "params" must be a tuple; got int instead'):
with pytest.raises(typeguard.TypeCheckError, match=re.escape('argument "params" (int) is not a tuple')):
DecimalType[4]

0 comments on commit 15ec1a7

Please sign in to comment.