Skip to content

Commit 242ab94

Browse files
committed
fix: a few typing issues
1 parent 1b57958 commit 242ab94

File tree

3 files changed

+29
-4
lines changed

3 files changed

+29
-4
lines changed

scim2_models/base.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ def check_request_attributes_mutability(
111111
"""Check and fix that the field mutability is expected according to the requests validation context, as defined in :rfc:`RFC7643 §7 <7653#section-7>`."""
112112
if (
113113
not info.context
114+
or not info.field_name
114115
or not info.context.get("scim")
115116
or not Context.is_request(info.context["scim"])
116117
):

scim2_models/rfc7643/schema.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,14 +70,17 @@ def make_python_model(
7070
Field(default=[obj.id]),
7171
)
7272

73+
if not obj.name:
74+
raise ValueError("Schema or Attribute 'name' must be defined")
75+
7376
model_name = to_pascal(to_snake(obj.name))
74-
model = create_model(model_name, __base__=base, **pydantic_attributes)
77+
model: type[T] = create_model(model_name, __base__=base, **pydantic_attributes) # type: ignore[call-overload]
7578

7679
# Set the ComplexType class as a member of the model
7780
# e.g. make Member an attribute of Group
7881
for attr_name in model.model_fields:
7982
attr_type = model.get_field_root_type(attr_name)
80-
if is_complex_attribute(attr_type):
83+
if attr_type and is_complex_attribute(attr_type):
8184
setattr(model, attr_type.__name__, attr_type)
8285

8386
return model
@@ -207,7 +210,7 @@ def from_python(cls, pytype) -> str:
207210
"""When an attribute is of type "complex", "subAttributes" defines a set of
208211
sub-attributes."""
209212

210-
def to_python(self) -> Optional[tuple[Any, Field]]:
213+
def to_python(self) -> Optional[tuple[Any, Any]]:
211214
"""Build tuple suited to be passed to pydantic 'create_model'."""
212215
if not self.name or not self.type:
213216
return None
@@ -290,7 +293,7 @@ def get_attribute(self, attribute_name: str) -> Optional[Attribute]:
290293
return attribute
291294
return None
292295

293-
def __getitem__(self, name: str) -> "Attribute":
296+
def __getitem__(self, name: str) -> "Attribute": # type: ignore[override]
294297
"""Find an attribute by its name."""
295298
if attribute := self.get_attribute(name):
296299
return attribute

tests/test_dynamic_schemas.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,17 @@
22
from typing import Annotated
33
from typing import Optional
44

5+
import pytest
6+
57
from scim2_models.annotations import Required
68
from scim2_models.context import Context
79
from scim2_models.rfc7643.enterprise_user import EnterpriseUser
810
from scim2_models.rfc7643.group import Group
911
from scim2_models.rfc7643.resource import Resource
1012
from scim2_models.rfc7643.resource_type import ResourceType
13+
from scim2_models.rfc7643.schema import Attribute
1114
from scim2_models.rfc7643.schema import Schema
15+
from scim2_models.rfc7643.schema import make_python_model
1216
from scim2_models.rfc7643.service_provider_config import ServiceProviderConfig
1317
from scim2_models.rfc7643.user import User
1418

@@ -177,3 +181,20 @@ class Bar(Foo):
177181
"urn:ietf:params:scim:schemas:core:2.0:Schema",
178182
],
179183
}
184+
185+
186+
def test_make_python_model_validates_name():
187+
"""Test that make_python_model raises an exception when obj.name is not defined."""
188+
# Test with Schema object without name
189+
schema = Schema(id="urn:example:schema")
190+
schema.name = None
191+
192+
with pytest.raises(ValueError, match="Schema or Attribute 'name' must be defined"):
193+
make_python_model(schema, Resource)
194+
195+
# Test with Attribute object without name
196+
attribute = Attribute(type=Attribute.Type.string)
197+
attribute.name = None
198+
199+
with pytest.raises(ValueError, match="Schema or Attribute 'name' must be defined"):
200+
make_python_model(attribute, Resource)

0 commit comments

Comments
 (0)