Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Core] Use frozenset for Python kTraitSet #90

Merged
merged 1 commit into from
Aug 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion RELEASE_NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@ v1.0.0-alpha.x
tested or published, whereas Python 3.11 is now published.
[OpenAssetIO#1351](https://github.com/OpenAssetIO/OpenAssetIO/issues/1351)

v1.0.0-alpha.9
- Changed the `kTraitSet` member of Specification classes in Python to
use the `frozenset` type, rather than standard `set`. This allows
the `kTraitSet` to be used as a dictionary key.
[#55](https://github.com/OpenAssetIO/OpenAssetIO-TraitGen/issues/55)`

`v1.0.0-alpha.9
--------------

### Breaking Changes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class {{ specification.id | to_py_class_name }}Specification:
Usage: {{ specification.usage | join(', ') }}
{% endif -%}
"""
kTraitSet = {
kTraitSet = frozenset({
{% for trait in specification.trait_set -%}
# '{{ trait.id }}'
{% if trait.package == package.id -%}
Expand All @@ -40,7 +40,7 @@ class {{ specification.id | to_py_class_name }}Specification:
{{ trait.package | to_py_module_name }}.traits.{{ trait.namespace | to_py_module_name }}.{{ trait.name | to_py_class_name }}Trait.kId,
{% endif -%}
{% endfor %}
}
})

def __init__(self, traitsData):
"""
Expand Down
16 changes: 16 additions & 0 deletions tests/generators/test_python.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,12 @@ def test_trait_set_composes_target_trait_kIds(self, module_all):
}
assert module_all.specifications.test.TwoLocalTraitsSpecification.kTraitSet == expected

# Ensure we can use the trait set as a dict key, i.e. it is a
# `frozenset`.
assert {module_all.specifications.test.TwoLocalTraitsSpecification.kTraitSet: True}[
module_all.specifications.test.TwoLocalTraitsSpecification.kTraitSet
] is True

def test_has_trait_getters_with_expected_docstring(self, module_all):
trait_one = module_all.traits.aNamespace.NoPropertiesTrait
trait_two = module_all.traits.anotherNamespace.NoPropertiesTrait
Expand Down Expand Up @@ -260,6 +266,11 @@ def test_trait_set_composes_target_trait_kIds(self, module_all, module_traits_on
module_traits_only.traits.test.AnotherTrait.kId,
}
assert module_all.specifications.test.OneExternalTraitSpecification.kTraitSet == expected
# Ensure we can use the trait set as a dict key, i.e. it is a
# `frozenset`.
assert {module_all.specifications.test.OneExternalTraitSpecification.kTraitSet: True}[
module_all.specifications.test.OneExternalTraitSpecification.kTraitSet
] is True

def test_has_trait_getters_with_expected_docstring(self, module_all, module_traits_only):
trait = module_traits_only.traits.test.AnotherTrait
Expand Down Expand Up @@ -294,6 +305,11 @@ def test_trait_set_composes_target_trait_kIds(self, module_all, module_traits_on
assert (
module_all.specifications.test.LocalAndExternalTraitSpecification.kTraitSet == expected
)
# Ensure we can use the trait set as a dict key, i.e. it is a
# `frozenset`.
assert {module_all.specifications.test.LocalAndExternalTraitSpecification.kTraitSet: True}[
module_all.specifications.test.LocalAndExternalTraitSpecification.kTraitSet
] is True

def test_has_trait_getters_with_expected_docstring(self, module_all, module_traits_only):
trait_one = module_all.traits.aNamespace.NoPropertiesTrait
Expand Down
Loading