Repository navigation
docs(bindings): recommend explicit None defaults for pyright - #80
Merged
Merged
Conversation
Pyright ignores Field(default=None) inside Annotated and reports every BindingModel field as required. No library-side change makes pyright see the default, so document `= None` and use it in examples and tests. Fixes #77 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
…g-defaults # Conflicts: # tests/test_bindings.py
Ruff 0.17 formats Python code blocks in Markdown, so the long binding field lines in README and CONTEXT are shortened. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.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.
Fixes #77
What
Pyright only recognises assigned defaults, so it ignores
Field(default=None)inside theUri/Literal[T]/RdfLiteralaliases and reports all binding model fields as required. This PR documents= Noneas the recommended pattern and applies it:BindingModelsection:= Nonein the snippets, plus a short explanation.CONTEXT.md: same, plus a note on why library-side fixes don't work.examples/(including13-undefined_values.pyfrom feat(bindings): add UNDEFINED sentinels for unbound binding values #78) andtests/test_bindings.py(including the models added by feat(bindings): add UNDEFINED sentinels for unbound binding values #78 and fix(bindings): compare serialized values in matches_with #79).= Nonebehaves like the implicit default for every field type, includingDatatype.The runtime default stays, so existing code without
= Nonekeeps working.Options explored (none work with pyright 1.1.414)
__init__(self, **data)onBindingModel: pyright still synthesizes a typed__init__for each subclass.ModelMetaclasssubclass with its owndataclass_transform: same result.dataclass_transformhas no "all fields optional" option, and defaults inAnnotatedare never considered.BindingModelfrom type checkers viaTYPE_CHECKING: rejected because it loses field-name and type checking.Testing
uv run pytestandruff: all pass.uvx pyright@1.1.414 examples tests/test_bindings.py: no "Argument(s) missing" errors left. The 27 remaining errors are tests that deliberately pass N3 strings,rdflib.Literalvalues or the wrongUNDEFINEDsentinel (from feat(bindings): add UNDEFINED sentinels for unbound binding values #78) to typed fields to check runtime validation. That is a separate typing limitation and out of scope here.Follow-ups (not in this PR)
AskAnswerKnowledgeInteraction/PostReactKnowledgeInteraction.prefixeshave the same root cause (Field(default_factory=dict)insideAnnotated). Pyright reportsprefixesas missing in 5 test call sites.knowledge_mapper/__init__.pyhas no__all__. Because the package shipspy.typed, downstream pyright users getreportPrivateImportUsageon every public import (from knowledge_mapper import BindingModel, ...).