Skip to content

Add --allow-words option to CLI for removing blacklisted field names#60

Open
EchterTimo wants to merge 2 commits intobogdandm:masterfrom
EchterTimo:allow-words
Open

Add --allow-words option to CLI for removing blacklisted field names#60
EchterTimo wants to merge 2 commits intobogdandm:masterfrom
EchterTimo:allow-words

Conversation

@EchterTimo
Copy link

@EchterTimo EchterTimo commented Mar 8, 2026

Hi guys!

I added --allow-words the arg to optionally remove specific words from the keyword blacklist

By default, field names that are also Python keywords or built-ins (e.g. id, type, hash) are renamed by appending _, resulting in awkward names like id_ or type_ in your models.
This is currently hardcoded in models.base.prepare_label

I think it's wrong to forbid people from using id or type if they really want.
So I think the arg is a good idea for these people to live their dreams. lol

The new --allow-words arg 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 type
# before
class User(BaseModel):
    id_: int = Field(..., alias="id")
    type_: str = Field(..., alias="type")

# after
class User(BaseModel):
    id: int
    type: str

Changes:

  • models/base.py: I added an allow_words parameter to prepare_label and GenericModelCodeGenerator
  • cli.py: I added a new --allow-words argument
  • README.md: I documented the new flag under CLI arguments with a disclaimer about possible syntax errors
  • test/test_cli/test_script.py: I added integration tests for the new feature

✅Tests executed. Everything works fine.

Copilot AI review requested due to automatic review settings March 8, 2026 23:35
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

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() / GenericModelCodeGenerator to accept allow_words and 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.

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