Add --allow-words option to CLI for removing blacklisted field names#60
Open
EchterTimo wants to merge 2 commits intobogdandm:masterfrom
Open
Add --allow-words option to CLI for removing blacklisted field names#60EchterTimo wants to merge 2 commits intobogdandm:masterfrom
EchterTimo wants to merge 2 commits intobogdandm:masterfrom
Conversation
There was a problem hiding this comment.
Pull request overview
Adds a new CLI option to control identifier blacklisting during model generation so specific “awkward” names (e.g. id, type, hash) can be kept without appending a trailing underscore.
Changes:
- Added
--allow-words WORD [WORD ...]to the CLI and wired it into generator kwargs. - Extended
prepare_label()/GenericModelCodeGeneratorto acceptallow_wordsand exclude them from blacklist-based renaming. - Documented the flag in the README and added CLI integration tests to cover the new behavior across frameworks.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
json_to_models/models/base.py |
Threads an allow_words set into name preparation and adjusts blacklist logic. |
json_to_models/cli.py |
Adds --allow-words argument and passes it into code generator kwargs. |
README.md |
Documents the new CLI flag and examples. |
test/test_cli/test_script.py |
Adds integration tests verifying underscore suppression and default behavior. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
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.
Hi guys!
I added
--allow-wordsthe arg to optionally remove specific words from the keyword blacklistBy default, field names that are also Python keywords or built-ins (e.g.
id,type,hash) are renamed by appending_, resulting in awkward names likeid_ortype_in your models.This is currently hardcoded in models.base.prepare_label
I think it's wrong to forbid people from using
idortypeif they really want.So I think the arg is a good idea for these people to live their dreams. lol
The new
--allow-wordsarg lets you exclude specific names from this behaviour so you have more control.Keep in mind that this argument is optional and does not change the current usage.
Example usage
json2models -m User user.json -f pydantic --allow-words id typeChanges:
models/base.py: I added anallow_wordsparameter toprepare_labelandGenericModelCodeGeneratorcli.py: I added a new--allow-wordsargumentREADME.md: I documented the new flag under CLI arguments with a disclaimer about possible syntax errorstest/test_cli/test_script.py: I added integration tests for the new feature✅Tests executed. Everything works fine.