Visitar URL original
refactor(ldap.schema): restore token_defaults with TypedDict annotations by spaceone · Pull Request #688 · python-ldap/python-ldap · GitHub
Skip to content

refactor(ldap.schema): restore token_defaults with TypedDict annotations - #688

Open
spaceone wants to merge 4 commits into
mainfrom
typing/schema-element-token-defaults
Open

spaceone wants to merge 4 commits into
mainfrom
typing/schema-element-token-defaults

Conversation

@spaceone

Copy link
Copy Markdown
Contributor

Reverts: 4277c52

@spaceone
spaceone force-pushed the typing/schema-element-token-defaults branch 3 times, most recently from 2cccfa3 to 23b4e23 Compare October 25, 2026 14:23
@spaceone spaceone added this to the 3.5.0 milestone Oct 25, 2026
@spaceone
spaceone force-pushed the typing/schema-element-token-defaults branch from 23b4e23 to b29584e Compare November 5, 2026 20:54
Comment thread Lib/ldap/schema/models.py


class Entry(EntryBase):
class Entry(UserDict[str, list[bytes]]):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After entry |= {'sn': [b'Smith']}, sn appears in iteration, but entry['sn'] and dict(entry) raise KeyError. The inherited UserDict.__ior__ writes directly to data, skipping Entry's attribute-name bookkeeping. Could we override it to call self.update(other) and return self?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fancy stuff. I added __ior__.

Comment thread Lib/ldap/schema/models.py Outdated
self._set_attrs(tokens, schema_element_attributes)
self.set_id(tokens[1])
opts = extract_tokens(tokens, self.token_defaults)
self._set_attrs(opts)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we need to retain the existing _set_attrs(tokens, opts) signature. Subclasses overriding that method now fail during construction because this call supplies only opts. Keeping both arguments preserves those subclasses while still allowing the TypedDict changes.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, changed it so that it has the unused token parameter.

python-ldap provides classes for all schema elements which exists to my knowledge.
So, I hope there is not a single subclass out there.

Comment thread Lib/ldap/schema/models.py Outdated
self.names = d.get('NAME', ())
def _set_attrs(self, opts: TokenDefaults) -> None:
opts = cast(SchemaElementTokens, opts)
self.names = opts['NAME']

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we keep opts.get('NAME', ()) here? A custom SchemaElement with token_defaults = {'DESC': (None,)} worked previously but now raises KeyError('NAME') during construction. IMO, an absent NAME should continue to produce an empty names tuple.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All internal schema elements define NAME - but if there is any subclass our there, I added that fallback.

Introduce a generic `_get_elements()` helper to preserve concrete schema
element types, eliminate repeated casts or asserts and improve type safety.
add overloads for `get_inheritedattr()` using Literal attribute names
to provide more precise return types for known schema attributes.
@spaceone
spaceone force-pushed the typing/schema-element-token-defaults branch from b29584e to 9317cb8 Compare November 13, 2026 19:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants