Skip to content

Commit 1a6ca6c

Browse files
committed
test: fix warnings
1 parent 72b24ff commit 1a6ca6c

10 files changed

+78
-52
lines changed

doc/tutorial.rst

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -389,11 +389,10 @@ If unset the default values will be :attr:`~scim2_models.Mutability.read_write`
389389
There is a dedicated type for :rfc:`RFC7643 §2.3.7 <7643#section-2.3.7>` :class:`~scim2_models.Reference`
390390
that can take type parameters to represent :rfc:`RFC7643 §7 'referenceTypes'<7643#section-7>`:
391391

392-
>>> from typing import Literal
393392
>>> class PetOwner(Resource):
394-
... pet: Reference[Literal["Pet"]]
393+
... pet: Reference["Pet"]
395394

396-
:class:`~scim2_models.Reference` has two special type parameters :data:`~scim2_models.ExternalReference` and :data:`~scim2_models.URIReference` that matches :rfc:`RFC7643 §7 <7643#section-7>` external and URI reference types.
395+
:class:`~scim2_models.Reference` has two special type parameters :data:`~scim2_models.External` and :data:`~scim2_models.URI` that matches :rfc:`RFC7643 §7 <7643#section-7>` external and URI reference types.
397396

398397
Dynamic schemas from models
399398
===========================

tests/test_dynamic_resources.py

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import datetime
2-
from typing import Literal
2+
from typing import Union
33

44
from pydantic import Base64Bytes
55

@@ -9,9 +9,9 @@
99
from scim2_models.annotations import Returned
1010
from scim2_models.annotations import Uniqueness
1111
from scim2_models.attributes import ComplexAttribute
12-
from scim2_models.reference import ExternalReference
12+
from scim2_models.reference import URI
13+
from scim2_models.reference import External
1314
from scim2_models.reference import Reference
14-
from scim2_models.reference import URIReference
1515
from scim2_models.resources.resource import Extension
1616
from scim2_models.resources.resource import Resource
1717
from scim2_models.resources.schema import Attribute
@@ -69,8 +69,7 @@ def test_make_group_model_from_schema(load_sample):
6969

7070
# Members.ref
7171
assert (
72-
Members.get_field_root_type("ref")
73-
== Reference[Literal["User"] | Literal["Group"]]
72+
Members.get_field_root_type("ref") == Reference[Union["User", "Group"]] # noqa: F821
7473
)
7574
assert not Members.get_field_multiplicity("ref")
7675
assert (
@@ -292,7 +291,7 @@ def test_make_user_model_from_schema(load_sample):
292291
assert User.get_field_annotation("nick_name", Uniqueness) == Uniqueness.none
293292

294293
# profile_url
295-
assert User.get_field_root_type("profile_url") == Reference[ExternalReference]
294+
assert User.get_field_root_type("profile_url") == Reference[External]
296295
assert not User.get_field_multiplicity("profile_url")
297296
assert (
298297
User.model_fields["profile_url"].description
@@ -642,7 +641,7 @@ def test_make_user_model_from_schema(load_sample):
642641
assert User.get_field_annotation("photos", Uniqueness) == Uniqueness.none
643642

644643
# photo.value
645-
assert Photos.get_field_root_type("value") == Reference[ExternalReference]
644+
assert Photos.get_field_root_type("value") == Reference[External]
646645
assert not Photos.get_field_multiplicity("value")
647646
assert Photos.model_fields["value"].description == "URL of a photo of the User."
648647
assert Photos.get_field_annotation("value", Required) == Required.false
@@ -859,8 +858,7 @@ def test_make_user_model_from_schema(load_sample):
859858

860859
# group.ref
861860
assert (
862-
Groups.get_field_root_type("ref")
863-
== Reference[Literal["User"] | Literal["Group"]]
861+
Groups.get_field_root_type("ref") == Reference[Union["User", "Group"]] # noqa: F821
864862
)
865863
assert not Groups.get_field_multiplicity("ref")
866864
assert (
@@ -1396,7 +1394,7 @@ def test_make_enterprise_user_model_from_schema(load_sample):
13961394
assert Manager.get_field_annotation("value", Uniqueness) == Uniqueness.none
13971395

13981396
# Manager.ref
1399-
assert Manager.get_field_root_type("ref") == Reference[Literal["User"]]
1397+
assert Manager.get_field_root_type("ref") == Reference["User"]
14001398
assert not Manager.get_field_multiplicity("ref")
14011399
assert (
14021400
Manager.model_fields["ref"].description
@@ -1482,7 +1480,7 @@ def test_make_resource_type_model_from_schema(load_sample):
14821480
)
14831481

14841482
# endpoint
1485-
assert ResourceType.get_field_root_type("endpoint") == Reference[URIReference]
1483+
assert ResourceType.get_field_root_type("endpoint") == Reference[URI]
14861484
assert not ResourceType.get_field_multiplicity("endpoint")
14871485
assert (
14881486
ResourceType.model_fields["endpoint"].description
@@ -1498,7 +1496,7 @@ def test_make_resource_type_model_from_schema(load_sample):
14981496
assert ResourceType.get_field_annotation("endpoint", Uniqueness) == Uniqueness.none
14991497

15001498
# schema
1501-
assert ResourceType.get_field_root_type("schema_") == Reference[URIReference]
1499+
assert ResourceType.get_field_root_type("schema_") == Reference[URI]
15021500
assert not ResourceType.get_field_multiplicity("schema_")
15031501
assert (
15041502
ResourceType.model_fields["schema_"].description
@@ -1543,7 +1541,7 @@ def test_make_resource_type_model_from_schema(load_sample):
15431541
)
15441542

15451543
# SchemaExtensions.schema
1546-
assert SchemaExtensions.get_field_root_type("schema_") == Reference[URIReference]
1544+
assert SchemaExtensions.get_field_root_type("schema_") == Reference[URI]
15471545
assert not SchemaExtensions.get_field_multiplicity("schema_")
15481546
assert (
15491547
SchemaExtensions.model_fields["schema_"].description
@@ -1630,7 +1628,7 @@ def test_make_service_provider_config_model_from_schema(load_sample):
16301628
# documentation_uri
16311629
assert (
16321630
ServiceProviderConfig.get_field_root_type("documentation_uri")
1633-
== Reference[ExternalReference]
1631+
== Reference[External]
16341632
)
16351633
assert not ServiceProviderConfig.get_field_multiplicity("documentation_uri")
16361634
assert (
@@ -2038,10 +2036,7 @@ def test_make_service_provider_config_model_from_schema(load_sample):
20382036
)
20392037

20402038
# authentication_schemes.spec_uri
2041-
assert (
2042-
AuthenticationSchemes.get_field_root_type("spec_uri")
2043-
== Reference[ExternalReference]
2044-
)
2039+
assert AuthenticationSchemes.get_field_root_type("spec_uri") == Reference[External]
20452040
assert not AuthenticationSchemes.get_field_multiplicity("spec_uri")
20462041
assert (
20472042
AuthenticationSchemes.model_fields["spec_uri"].description
@@ -2071,7 +2066,7 @@ def test_make_service_provider_config_model_from_schema(load_sample):
20712066
# authentication_schemes.documentation_uri
20722067
assert (
20732068
AuthenticationSchemes.get_field_root_type("documentation_uri")
2074-
== Reference[ExternalReference]
2069+
== Reference[External]
20752070
)
20762071
assert not AuthenticationSchemes.get_field_multiplicity("documentation_uri")
20772072
assert (

tests/test_dynamic_schemas.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import operator
2-
from typing import Annotated
32

43
import pytest
54

6-
from scim2_models.annotations import Required
5+
from scim2_models import URN
76
from scim2_models.context import Context
87
from scim2_models.resources.enterprise_user import EnterpriseUser
98
from scim2_models.resources.group import Group
@@ -116,9 +115,7 @@ def test_inheritance():
116115
"""Check that parent attributes are included in the schema."""
117116

118117
class Foo(Resource):
119-
schemas: Annotated[list[str], Required.true] = [
120-
"urn:ietf:params:scim:schemas:core:2.0:Foo"
121-
]
118+
__schema__ = URN("urn:ietf:params:scim:schemas:core:2.0:Foo")
122119

123120
foo: str | None = None
124121

tests/test_errors.py

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,37 @@
1+
import warnings
2+
3+
from scim2_models.exceptions import InvalidFilterException
4+
from scim2_models.exceptions import InvalidPathException
5+
from scim2_models.exceptions import InvalidSyntaxException
6+
from scim2_models.exceptions import InvalidValueException
7+
from scim2_models.exceptions import InvalidVersionException
8+
from scim2_models.exceptions import MutabilityException
9+
from scim2_models.exceptions import NoTargetException
10+
from scim2_models.exceptions import SensitiveException
11+
from scim2_models.exceptions import TooManyException
12+
from scim2_models.exceptions import UniquenessException
113
from scim2_models.messages.error import Error
214

315

416
def test_predefined_errors():
5-
for gen in (
17+
for exc in (
18+
InvalidFilterException(),
19+
TooManyException(),
20+
UniquenessException(),
21+
MutabilityException(),
22+
InvalidSyntaxException(),
23+
InvalidPathException(),
24+
NoTargetException(),
25+
InvalidValueException(),
26+
InvalidVersionException(),
27+
SensitiveException(),
28+
):
29+
assert isinstance(exc.to_error(), Error)
30+
31+
32+
def test_deprecated_make_error_methods():
33+
"""Test deprecated make_*_error class methods emit warnings."""
34+
deprecated_methods = (
635
Error.make_invalid_filter_error,
736
Error.make_too_many_error,
837
Error.make_uniqueness_error,
@@ -13,5 +42,12 @@ def test_predefined_errors():
1342
Error.make_invalid_value_error,
1443
Error.make_invalid_version_error,
1544
Error.make_sensitive_error,
16-
):
17-
assert isinstance(gen(), Error)
45+
)
46+
for method in deprecated_methods:
47+
with warnings.catch_warnings(record=True) as w:
48+
warnings.simplefilter("always")
49+
result = method()
50+
assert isinstance(result, Error)
51+
assert len(w) == 1
52+
assert issubclass(w[0].category, DeprecationWarning)
53+
assert "deprecated" in str(w[0].message)

tests/test_model_attributes.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import uuid
22
from typing import Annotated
33

4-
from scim2_models.annotations import Required
4+
from scim2_models import URN
55
from scim2_models.annotations import Returned
66
from scim2_models.attributes import ComplexAttribute
77
from scim2_models.base import BaseModel
@@ -23,7 +23,8 @@ class Sub(ComplexAttribute):
2323

2424

2525
class Sup(Resource):
26-
schemas: Annotated[list[str], Required.true] = ["urn:example:2.0:Sup"]
26+
__schema__ = URN("urn:example:2.0:Sup")
27+
2728
dummy: str
2829
sub: Sub
2930
subs: list[Sub]
@@ -51,23 +52,26 @@ class Baz(ComplexAttribute):
5152

5253

5354
class Foo(Resource):
54-
schemas: Annotated[list[str], Required.true] = ["urn:example:2.0:Foo"]
55+
__schema__ = URN("urn:example:2.0:Foo")
56+
5557
sub: Annotated[ReturnedModel, Returned.default]
5658
bar: str
5759
snake_case: str
5860
baz: Baz | None = None
5961

6062

6163
class Bar(Resource):
62-
schemas: Annotated[list[str], Required.true] = ["urn:example:2.0:Bar"]
64+
__schema__ = URN("urn:example:2.0:Bar")
65+
6366
sub: Annotated[ReturnedModel, Returned.default]
6467
bar: str
6568
snake_case: str
6669
baz: Baz | None = None
6770

6871

6972
class MyExtension(Extension):
70-
schemas: Annotated[list[str], Required.true] = ["urn:example:2.0:MyExtension"]
73+
__schema__ = URN("urn:example:2.0:MyExtension")
74+
7175
baz: str
7276

7377

tests/test_model_serialization.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import pytest
44

5+
from scim2_models import URN
56
from scim2_models.annotations import Mutability
67
from scim2_models.annotations import Required
78
from scim2_models.annotations import Returned
@@ -18,7 +19,7 @@ class SubRetModel(ComplexAttribute):
1819

1920

2021
class SupRetResource(Resource):
21-
schemas: Annotated[list[str], Required.true] = ["urn:org:example:SupRetResource"]
22+
__schema__ = URN("urn:org:example:SupRetResource")
2223

2324
always_returned: Annotated[str | None, Returned.always] = None
2425
never_returned: Annotated[str | None, Returned.never] = None

tests/test_patch_op_extensions.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
from typing import TypeVar
22

33
import pytest
4-
from pydantic import Field
54
from pydantic import ValidationError
65

6+
from scim2_models import URN
77
from scim2_models import Group
88
from scim2_models import GroupMember
99
from scim2_models import InvalidPathException
@@ -262,8 +262,8 @@ def test_create_parent_object_return_none():
262262
T = TypeVar("T")
263263

264264
class TestResourceTypeVar(Resource):
265-
schemas: list[str] = Field(default=["urn:test:TestResource"])
266-
# TypeVar is not a class, so _create_parent_object should return None
265+
__schema__ = URN("urn:test:TestResource")
266+
267267
typevar_field: T = None
268268

269269
user = TestResourceTypeVar()

tests/test_patch_op_replace.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
from typing import Annotated
22

33
import pytest
4-
from pydantic import Field
54
from pydantic import ValidationError
65

6+
from scim2_models import URN
77
from scim2_models import PatchOp
88
from scim2_models import PatchOperation
99
from scim2_models import User
@@ -208,7 +208,8 @@ def test_immutable_field():
208208
"""Test that replace operations on immutable fields raise validation errors."""
209209

210210
class Dummy(Resource):
211-
schemas: list[str] = Field(default=["urn:test:TestResource"])
211+
__schema__ = URN("urn:test:TestResource")
212+
212213
immutable: Annotated[str, Mutability.immutable]
213214

214215
with pytest.raises(ValidationError, match="mutability"):

tests/test_resource_extension.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
import datetime
2-
from typing import Annotated
32

43
import pytest
54

5+
from scim2_models import URN
66
from scim2_models import Context
77
from scim2_models import EnterpriseUser
88
from scim2_models import Extension
99
from scim2_models import Manager
1010
from scim2_models import Meta
11-
from scim2_models import Required
1211
from scim2_models import User
1312

1413

@@ -252,9 +251,7 @@ def test_invalid_delitem():
252251

253252

254253
class SuperHero(Extension):
255-
schemas: Annotated[list[str], Required.true] = [
256-
"urn:example:extensions:2.0:SuperHero"
257-
]
254+
__schema__ = URN("urn:example:extensions:2.0:SuperHero")
258255

259256
superpower: str | None = None
260257
"""The superhero superpower."""

tests/test_resource_type.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
from typing import Annotated
2-
1+
from scim2_models import URN
32
from scim2_models import EnterpriseUser
43
from scim2_models import Extension
54
from scim2_models import Reference
6-
from scim2_models import Required
75
from scim2_models import ResourceType
86
from scim2_models import User
97

@@ -69,9 +67,7 @@ def test_from_resource_with_extensions():
6967

7068
def test_from_resource_with_mulitple_extensions():
7169
class TestExtension(Extension):
72-
schemas: Annotated[list[str], Required.true] = [
73-
"urn:ietf:params:scim:schemas:extension:Test:1.0:User"
74-
]
70+
__schema__ = URN("urn:ietf:params:scim:schemas:extension:Test:1.0:User")
7571

7672
test: str | None = None
7773
test2: list[str] | None = None

0 commit comments

Comments
 (0)