Skip to content

Commit 8e30112

Browse files
committed
feat: implement Schema[attribute_name] to access schema attributes
1 parent 7755737 commit 8e30112

File tree

3 files changed

+28
-2
lines changed

3 files changed

+28
-2
lines changed

doc/changelog.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ Changelog
44
[0.3.0] - Unreleased
55
--------------------
66

7+
Added
8+
^^^^^
9+
- :meth:`Attribute.get_attribute <scim2_models.Attribute.get_attribute>` can be called with brackets.
10+
711
Changed
812
^^^^^^^
913
- Add a :paramref:`~scim2_models.BaseModel.model_validate.original`

scim2_models/rfc7643/schema.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,12 @@ def get_attribute(self, attribute_name: str) -> Optional["Attribute"]:
245245
return sub_attribute
246246
return None
247247

248+
def __getitem__(self, name):
249+
"""Find an attribute by its name."""
250+
if attribute := self.get_attribute(name):
251+
return attribute
252+
raise KeyError(f"This attribute has no '{name}' sub-attribute")
253+
248254

249255
class Schema(Resource):
250256
schemas: Annotated[list[str], Required.true] = [
@@ -280,3 +286,9 @@ def get_attribute(self, attribute_name: str) -> Optional[Attribute]:
280286
if attribute.name == attribute_name:
281287
return attribute
282288
return None
289+
290+
def __getitem__(self, name):
291+
"""Find an attribute by its name."""
292+
if attribute := self.get_attribute(name):
293+
return attribute
294+
raise KeyError(f"This schema has no '{name}' attribute")

tests/test_schema.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,13 +92,18 @@ def test_get_schema_attribute(load_sample):
9292
payload = load_sample("rfc7643-8.7.1-schema-user.json")
9393
schema = Schema.model_validate(payload)
9494
assert schema.get_attribute("invalid") is None
95+
with pytest.raises(KeyError):
96+
schema["invalid"]
9597

9698
assert schema.attributes[0].name == "userName"
9799
assert schema.attributes[0].mutability == Mutability.read_write
98-
schema.get_attribute("userName").mutability = Mutability.read_only
99100

101+
schema.get_attribute("userName").mutability = Mutability.read_only
100102
assert schema.attributes[0].mutability == Mutability.read_only
101103

104+
schema["userName"].mutability = Mutability.read_write
105+
assert schema.attributes[0].mutability == Mutability.read_write
106+
102107

103108
def test_get_attribute_attribute(load_sample):
104109
"""Test the Schema.get_attribute method."""
@@ -107,9 +112,14 @@ def test_get_attribute_attribute(load_sample):
107112
attribute = schema.get_attribute("members")
108113

109114
assert attribute.get_attribute("invalid") is None
115+
with pytest.raises(KeyError):
116+
attribute["invalid"]
110117

111118
assert attribute.sub_attributes[0].name == "value"
112119
assert attribute.sub_attributes[0].mutability == Mutability.immutable
113-
attribute.get_attribute("value").mutability = Mutability.read_only
114120

121+
attribute.get_attribute("value").mutability = Mutability.read_only
115122
assert attribute.sub_attributes[0].mutability == Mutability.read_only
123+
124+
attribute["value"].mutability = Mutability.read_write
125+
assert attribute.sub_attributes[0].mutability == Mutability.read_write

0 commit comments

Comments
 (0)