Repository navigation
Conversation
`from_str` (with a string value), `isinstance` and `obj` join the keywords; `re`, `len` and `cast` are added as plain properties next to the `pattern`, `minItems` and `minLength` checks that call them. In Python's `from_dict` each becomes a local variable that shadows what it is named after. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
`from_dict` assigns each property to a local variable of the property's name, so a property named like something `from_dict` uses shadowed it for the whole function: a converter it calls (`from_date` in `misc/a0496.json` gave `UnboundLocalError`, and mypy `"date" not callable`), its parameter `obj`, `isinstance`, or what the constraint checks call (`len`, `re`, `cast`). Reserve those names for properties. The converters come from a `Record<ConverterFunction, boolean>` of the ones `from_dict` calls, named as `conv` names them, so a new converter cannot be missed; names only `to_dict` uses (`to_class`, `to_enum`, `to_float`, `is_type`) are not reserved. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This branch has not been deployed
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.
Description
JSONPythonRenderer.forbiddenForObjectPropertiesnow reserves, forproperties, every name that
from_dictuses besides its own locals:obj, andisinstance;from_none,from_bool,from_int,from_float,from_str,from_list,from_union,from_date,from_time,from_datetime,from_stringified_bool;from_dictwas already reserved);from_dictcall:len(
minItems,maxItems,minLength,maxLength),re(pattern)and
cast.The converter names come from a
Record<ConverterFunction, boolean>thatsays which converters
from_dictcalls. They are named by the samefunction as
conv(nowconverterName), so a new converter has to beadded to the record and cannot be missed. The converters that only
to_dictcalls (to_float,to_enum,to_class,is_type) shadownothing and are not reserved. The constraint checks now use constants
for
len,reandcast, and the reserved list uses the sameconstants.
A test commit adds inputs that cover these names:
test/keywords.txtgetsfrom_str(with a string value, so theproperty calls
from_str),isinstanceandobj.keywords.json,keyword-enum.schemaandkeyword-unions.schemawere regenerated withtest/make-keyword-tests.sh.pattern.schema,min-max-items.schemaandminmaxlength.schemaeachget an optional string property
re,lenorcast, and their.1.jsonsamples get a value for it.
Related Issue
Fixes #3550
Motivation and Context
from_dictassigns each property to a local variable named after theproperty, and that local shadows the name for the whole function. So a
property named
from_date(as intest/inputs/json/misc/a0496.json)gives
UnboundLocalErrorat run time, and mypy reports"date" not callable. Properties namedobj,isinstance,len,reor
castbreakfrom_dictthe same way.Previous Behaviour / Output
New Behaviour / Output
The JSON name stays
from_date. A reserved property gets renamed likeany other reserved name, so the Python attribute name changes for
existing users with properties named
obj,isinstance,from_*,len,reorcast. Of the repository's inputs at master, onlya0496.json's Python output changes.The new keywords and properties are test inputs for every language, so
the other languages' outputs for
keywords.json, the two keyword schemasand the three constraint schemas change too.
How Has This Been Tested?
Environment: macOS 26.6 (arm64), Node 24.16.0, Python 3.12.16 with
mypy 2.4.1. The other languages ran with OpenJDK 21.0.11 and Maven 3.10.0,
Ruby 4.0.6, Apple clang 21 (C, C++, Objective-C), Swift, Rust (cargo)
and Dart.
On this branch:
npm ci,npm run build,npm run lint(passes),npm run test:unit(54 files, 241 tests pass).
QUICKTEST=true FIXTURE=python,schema-python npm run test:fixtures:all 156 runs pass.
FIXTURE=python npm run test:fixtures -- test/inputs/json/misc/a0496.jsonand
test/inputs/json/priority/keywords.json: pass.QUICKTEST=true FIXTURE=typescript,javascript,ruby,cjson,javaonkeywords.json, andschema-typescript,schema-javascript,schema-ruby,schema-cjson,schema-javaon
pattern.schema,min-max-items.schema,minmaxlength.schema,keyword-enum.schemaandkeyword-unions.schema: all pass.swift,rust,dart,typescript-zod,typescript-effect-schema,flow,javascript-prop-types,objective-candcplusplus(and theirschema-fixtures, plusschema-java-datetime-legacy): all pass.With only the test commit (the fix not applied):
pythonfails onkeywords.json(Name "isinstance" is used before definition)and on
misc/a0496.json("date" not callable).schema-pythonfails onpattern.schema(Name "re" is used before definition),min-max-items.schema(len),minmaxlength.schema(cast) andkeyword-unions.schema(isinstance).keyword-enum.schemapasses.Not run here, because the toolchains are not installed: C#, Go, Kotlin,
Scala, Elm, PHP, Elixir, Haskell, Crystal and Pike.
Screenshots (if appropriate):
n/a
Written by Claude (an AI assistant made by Anthropic) and posted on behalf of the account owner (EloB). The reproduction, fix and tests were checked by running them as described above.
🤖 Generated with Claude Code