-
Notifications
You must be signed in to change notification settings - Fork 20
[BUG] Divisions: allow both is_land and is_territorial to be true #546
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
Merged
Dana Bauer (danabauer)
merged 11 commits into
main
from
atiannicelli/allow-both-is-land-is-territorial
Jul 8, 2026
Merged
Changes from 2 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
9bf6cc2
Allow both is_land and is_territorial to be true
atiannicelli eb77b91
Fix lint formatting in model_constraints.py
atiannicelli 45beeda
Potential fix for pull request finding
atiannicelli e777a8d
Potential fix for pull request finding
atiannicelli 777a4ea
Add require_any_true constraint tests
atiannicelli 3df9e21
Fix pytest 9.1 test deprecations
atiannicelli 48063d3
Generalize require_any_true conditions
atiannicelli 1eb1dec
Add reference division examples
atiannicelli c4b7235
Fix mypy narrowing for require_any_true
atiannicelli 3b64d0a
Fix mypy typing for require_any_true test
atiannicelli b6836cf
Merge branch 'main' into atiannicelli/allow-both-is-land-is-territorial
atiannicelli File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
18 changes: 0 additions & 18 deletions
18
counterexamples/divisions/division_boundary/bad-not-both.yaml
This file was deleted.
Oops, something went wrong.
27 changes: 27 additions & 0 deletions
27
examples/divisions/division_area/both_land_territorial.yaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| --- | ||
| id: example:division_area:both_land_territorial:country:us | ||
| type: Feature | ||
| geometry: | ||
| type: Polygon | ||
| coordinates: [ | ||
| [ | ||
| [-82.8732511, 24.4116731], | ||
| [-82.5948517, 24.5902399], | ||
| [-82.7300073, 24.8395704], | ||
| [-83.153058, 24.6776636], | ||
| [-82.8732511, 24.4116731] | ||
| ] | ||
| ] | ||
| properties: | ||
| theme: divisions | ||
| type: division_area | ||
| subtype: country | ||
| is_land: true | ||
| is_territorial: true | ||
| country: US | ||
| admin_level: 0 | ||
| version: 0 | ||
| class: land | ||
| division_id: example:division:country:us | ||
| names: | ||
| primary: United States |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
152 changes: 152 additions & 0 deletions
152
...es/overture-schema-system/src/overture/schema/system/model_constraint/require_any_true.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,152 @@ | ||
| """ | ||
| Require at least one field in a group of `bool` fields to have the value `True`. | ||
| """ | ||
|
|
||
| from collections.abc import Callable | ||
| from types import NoneType, UnionType | ||
| from typing import Annotated, Any, Union, get_args, get_origin | ||
|
|
||
| from pydantic import BaseModel, ConfigDict | ||
| from typing_extensions import override | ||
|
|
||
| from .._json_schema import get_static_json_schema_extra, put_any_of | ||
| from .model_constraint import FieldGroupConstraint, apply_alias | ||
|
|
||
|
|
||
| def require_any_true(*field_names: str) -> Callable[[type[BaseModel]], type[BaseModel]]: | ||
| """ | ||
| Decorate a Pydantic model class with a constraint requiring that at least one field in a group | ||
| of `bool` fields has the value `True`. | ||
|
|
||
| This function is the decorator version of the `RequireAnyTrueConstraint` class. | ||
|
|
||
| Unlike `radio_group`, which requires *exactly one* field to be `True`, this constraint allows | ||
| multiple fields to be `True` simultaneously. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| *field_names : str | ||
| Varargs list of at least two unique field names. | ||
|
|
||
| Returns | ||
| ------- | ||
| Callable | ||
| Decorator factory | ||
|
|
||
| Example | ||
| ------- | ||
| >>> from pydantic import BaseModel, ValidationError | ||
| >>> | ||
| >>> @require_any_true("foo", "bar") | ||
| ... class MyModel(BaseModel): | ||
| ... foo: bool | None = None | ||
| ... bar: bool = True | ||
| ... | ||
| >>> MyModel() # validates OK | ||
| MyModel(foo=None, bar=True) | ||
| >>> MyModel(foo=True, bar=True) # validates OK | ||
| MyModel(foo=True, bar=True) | ||
| >>> MyModel(foo=True, bar=False) # validates OK | ||
| MyModel(foo=True, bar=False) | ||
| >>> | ||
| >>> try: | ||
| ... MyModel(bar=False) | ||
| ... except ValidationError as e: | ||
| ... assert ( | ||
| ... "at least one field from the `bool` field group [foo, bar] must be True, " | ||
| ... "but none is True" | ||
| ... ) in str(e) | ||
| ... print("Validation failed") | ||
| Validation failed | ||
| """ | ||
| model_constraint = RequireAnyTrueConstraint._create_internal( | ||
| f"@{require_any_true.__name__}", *field_names | ||
| ) | ||
|
|
||
| return model_constraint.decorate | ||
|
|
||
|
|
||
| class RequireAnyTrueConstraint(FieldGroupConstraint): | ||
| """ | ||
| Class implementing the `require_any_true` decorator, which can also be used standalone. | ||
| """ | ||
|
atiannicelli marked this conversation as resolved.
Outdated
|
||
|
|
||
| def __init__(self, *field_names: str): | ||
| super().__init__( | ||
| None, RequireAnyTrueConstraint.__validate_field_names(field_names) | ||
| ) | ||
|
|
||
| @classmethod | ||
| def _create_internal( | ||
| cls, name: str, *field_names: str | ||
| ) -> "RequireAnyTrueConstraint": | ||
| instance = cls.__new__(cls) | ||
| super(RequireAnyTrueConstraint, instance).__init__( | ||
| name, RequireAnyTrueConstraint.__validate_field_names(field_names) | ||
| ) | ||
| return instance | ||
|
|
||
| @staticmethod | ||
| def __validate_field_names(field_names: tuple[str, ...]) -> tuple[str, ...]: | ||
| if len(field_names) < 2: | ||
| raise ValueError( | ||
| f"`field_names` must contain at least two items, but {field_names} has only {len(field_names)}" | ||
| ) | ||
| return field_names | ||
|
|
||
| @override | ||
| def validate_class(self, model_class: type[BaseModel]) -> None: | ||
| super().validate_class(model_class) | ||
|
|
||
| def is_bool(annotation: type[Any] | None) -> bool: | ||
| if annotation is bool: | ||
| return True | ||
| origin = get_origin(annotation) | ||
| if origin is Annotated: | ||
| return is_bool(get_args(annotation)[0]) | ||
| elif get_origin(annotation) in (Union, UnionType): | ||
| args = get_args(annotation) | ||
| return any(is_bool(a) for a in args) and all( | ||
| is_bool(a) or a in (None, NoneType) for a in args | ||
| ) | ||
| else: | ||
| return False | ||
|
|
||
| non_bool_fields = [ | ||
| f | ||
| for f in self.field_names | ||
| if not is_bool(model_class.model_fields[f].annotation) | ||
| ] | ||
| if non_bool_fields: | ||
| raise TypeError( | ||
| f"`{self.name}` specifies fields that are have a non-`bool` type in the model class `{model_class.__name__}`: {', '.join(non_bool_fields)} " | ||
| ) | ||
|
Copilot marked this conversation as resolved.
Outdated
|
||
|
|
||
| @override | ||
| def validate_instance(self, model_instance: BaseModel) -> None: | ||
| super().validate_instance(model_instance) | ||
|
|
||
| true_fields = [ | ||
| f for f in self.field_names if getattr(model_instance, f) is True | ||
| ] | ||
|
|
||
| if len(true_fields) >= 1: | ||
| return | ||
|
|
||
| raise ValueError( | ||
| f"at least one field from the `bool` field group [{', '.join(self.field_names)}] " | ||
| f"must be True, but none is True (`{self.name}`)" | ||
| ) | ||
|
|
||
| @override | ||
| def edit_config(self, model_class: type[BaseModel], config: ConfigDict) -> None: | ||
| super().edit_config(model_class, config) | ||
|
|
||
| json_schema = get_static_json_schema_extra(config) | ||
|
|
||
| def has_true_value(field_name: str) -> dict: | ||
| return { | ||
| "properties": {apply_alias(model_class, field_name): {"const": True}} | ||
| } | ||
|
|
||
| put_any_of(json_schema, [has_true_value(f) for f in self.field_names]) | ||
|
Copilot marked this conversation as resolved.
Outdated
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.