Skip to content

Prepend a prefix to field names to allow use as Python identifiers #206

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Oct 7, 2020

Conversation

kalzoo
Copy link
Contributor

@kalzoo kalzoo commented Oct 6, 2020

This change allows the use of field names with leading characters (such as digits) which if converted directly into Python code would yield invalid identifiers.

Closes #203

@codecov
Copy link

codecov bot commented Oct 6, 2020

Codecov Report

❗ No coverage uploaded for pull request base (main@461396d). Click here to learn what that means.
The diff coverage is 100.00%.

Impacted file tree graph

@@           Coverage Diff            @@
##             main      #206   +/-   ##
========================================
  Coverage        ?   100.00%           
========================================
  Files           ?        41           
  Lines           ?      1299           
  Branches        ?         0           
========================================
  Hits            ?      1299           
  Misses          ?         0           
  Partials        ?         0           
Impacted Files Coverage Δ
openapi_python_client/config.py 100.00% <100.00%> (ø)
openapi_python_client/parser/properties.py 100.00% <100.00%> (ø)
openapi_python_client/utils.py 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 461396d...8d61be5. Read the comment docs.

@kalzoo kalzoo force-pushed the 203-invalid-identifiers branch from 53aced9 to 0bb503f Compare October 6, 2020 18:45
if value.isidentifier():
return value

new_value = f"field_{value}"
Copy link
Contributor Author

Choose a reason for hiding this comment

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

note: as a user I'd prefer this to be configurable, but didn't notice a simple path for a Config to get here

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yeah config right now is pretty icky- bunch of global mutations. I'll fix it eventually 😅

if new_value.isidentifier():
return new_value

raise ValueError(f"Cannot convert {value} to a valid python identifier")
Copy link
Contributor Author

Choose a reason for hiding this comment

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

We can get craftier here and replace other invalid characters within the string as well

@dbanty
Copy link
Collaborator

dbanty commented Oct 6, 2020

@kalzoo thanks for the PR, I'll check it out a bit later! I just wanted to pop in to let you know to expect CircleCI to fail on Python 3.9. I added test coverage but it seems like something in Pydantic doesn't get along with it right now.

Copy link
Collaborator

@dbanty dbanty left a comment

Choose a reason for hiding this comment

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

Thanks for your work on this @kalzoo! I made a couple tweaks, feel free to take a look and let me know if you disagree with anything before I merge in.

Specifically:

  1. Identifiers more aggressively strip disallowed characters so that the ValueError isn't necessary
  2. The prefix is now configurable (as you recommended)

Copy link
Contributor Author

@kalzoo kalzoo left a comment

Choose a reason for hiding this comment

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

Good edits!

Just the one comment - otherwise, I'm happy with it. Thanks for the quick followups!


if new_value.isidentifier():
return new_value

raise ValueError(f"Cannot convert {value} to a valid python identifier")
return f"{FIELD_PREFIX}_{new_value}"
Copy link
Contributor Author

Choose a reason for hiding this comment

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

  1. I'd suggest including the _ in the prefix itself (i.e. field_) rather than here, so the user can opt to omit it

  2. This could still be an invalid identifier - one case being that FIELD_PREFIX itself has invalid characters. This could be checked here, in Config, or not at all

Copy link
Collaborator

Choose a reason for hiding this comment

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

Good call, I've made the config change so people can omit the underscore if they want. For now I don't want to try and catch user-supplied errors, I'm pretty sure invalid classes can also be provided from the other config values in the current version.

@dbanty dbanty merged commit e801b52 into openapi-generators:main Oct 7, 2020
@dbanty dbanty added this to the 0.6.2 milestone Oct 7, 2020
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.

Invalid identifiers generated
2 participants