feat(models): add type-safe field name references for Pydantic DTOs#108
Merged
HosseinNejatiJavaremi merged 1 commit intoSyntaxArc:masterfrom Feb 23, 2026
Conversation
Add FieldStr utility class and BaseMeta metaclass to BaseDTO so that
all DTO subclasses automatically expose field names as type-safe class
attributes (e.g., PaginationDTO.page returns FieldStr("page")).
This enables IDE autocompletion, refactoring support, and eliminates
hardcoded field name strings. Instance attribute access is unaffected.
- Add FieldStr(str) with __slots__ for memory efficiency
- Add BaseMeta(ModelMetaclass) that sets FieldStr attrs after class construction
- Replace hardcoded field_name strings in range_dtos.py model validators
- Add BDD feature file with 6 scenarios covering FieldStr behavior
Co-authored-by: Cursor <cursoragent@cursor.com>
HosseinNejatiJavaremi
added a commit
that referenced
this pull request
Feb 23, 2026
Add comprehensive changelog entry for the type-safe field name references feature merged in PR #108, including FieldStr utility and BaseMeta metaclass implementation details. Co-authored-by: Cursor <cursoragent@cursor.com>
HosseinNejatiJavaremi
added a commit
that referenced
this pull request
Feb 23, 2026
- Add v4.3.3 section documenting reversion of type-safe field references feature - Document removal of FieldStr utility class and BaseMeta metaclass - Clarify that v4.3.3 supersedes the cancelled v4.3.2 release - Feature is being reconsidered for future implementation with improved design Co-authored-by: Cursor <cursoragent@cursor.com>
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Add FieldStr utility class and BaseMeta metaclass to BaseDTO so that all DTO subclasses automatically expose field names as type-safe class attributes (e.g., PaginationDTO.page returns FieldStr("page")).
This enables IDE autocompletion, refactoring support, and eliminates hardcoded field name strings. Instance attribute access is unaffected.
Description
Hardcoded field name strings (e.g.,
"from_","to") scattered across DTOs and validators are fragile — renaming a field won't cause a lint or type error, leading to silent runtime bugs.This PR adds a
FieldStr(str)utility and aBaseMeta(ModelMetaclass)metaclass toBaseDTO. After Pydantic'sModelMetaclassconstructs the class and populatesmodel_fields,BaseMetaoverwrites the corresponding class attributes withFieldStrinstances so thatMyDTO.field_namereturns a type-safe string equal to"field_name". Instance attribute access is unaffected because Python resolves instance__dict__entries before class attributes.How It Works
Class-level access returns FieldStr (a str subclass)
Instance-level access is unaffected
Two hardcoded field name strings in
range_dtos.pymodel validators have been replaced with type-safeFieldStrreferences as a demonstration.Type of change
How Has This Been Tested?
features/base_dtos.featurecovering: FieldStr class attributes exist, instance access returns actual values, FieldStr works as dict key, FieldStr works in string comparisons, inherited DTOs get FieldStr attributes, existing PaginationDTO has FieldStr attributesChecklist:
Additional context
The primary value of this feature is for downstream consumers of ArchiPy who define their own DTOs and can reference field names type-safely in their application code (queries, filters, serialization keys, etc.). Note that
@field_validator("field_name")string arguments cannot be replaced because the decorator executes before the class (and thus the metaclass) is fully constructed — this is a Pydantic limitation.