Skip to content
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

[BUG] [python-flask] A backslash-character pair generates a SyntaxWarning from Python 3.12 #19487

Open
5 of 6 tasks
jops-wtakase opened this issue Aug 30, 2024 · 0 comments
Open
5 of 6 tasks

Comments

@jops-wtakase
Copy link

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

When I run the python-flask generator, some generated models output SyntaxWarning in Python 3.12. This issue is caused by the {{{pattern}}} in the line here, which may contain a backslash (\), making the string a potential source of warnings.

Introduced feature in Python 3.12

Starting from Python 3.12, a backslash-character pair generates a SyntaxWarning instead of a DeprecationWarning. In future Python versions, this will be escalated to a SyntaxError.

openapi-generator version

Latest master branch (1518237)

OpenAPI declaration file content or url

example.yaml

openapi: "3.0.3"
info:
  title: Example
  version: 1.0.0
servers: 
  - url: "http://localhost:3000"
paths: 
 "/example":
    get: 
      responses:
        '200':
          description: example response
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Example'
components:
  schemas:
    Example:
      title: Example
      description: Example
      type: object
      properties:
        name:
          type: string
          pattern: '^[a-zA-Z0-9]+[a-zA-Z0-9\.\-_]*[a-zA-Z0-9]+$'
Generation Details
java -jar openapi-generator-cli.jar \
    generate \
    -i /tmp/example.yaml \
    -g python-flask \
    --additional-properties packageName=example \
    -o /tmp/example
Steps to reproduce
docker run -v /tmp/example/example/models/example.py:/example.py -w /models --rm python:3.12-alpine python -Wa -m py_compile /example.py

/example.py:63: SyntaxWarning: invalid escape sequence '\.'
  raise ValueError("Invalid value for `name`, must be a follow pattern or equal to `/^[a-zA-Z0-9]+[a-zA-Z0-9\.\-_]*[a-zA-Z0-9]+$/`")  # noqa: E501
Related issues/PRs

None found. Searched using the keyword SyntaxWarning, but no relevant issues/PRs were identified.

Suggest a fix

Modify the line in modules/openapi-generator/src/main/resources/python-flask/model.mustache from:

            raise ValueError("Invalid value for `{{name}}`, must be a follow pattern or equal to `{{{pattern}}}`")  # noqa: E501

to:

            raise ValueError(r"Invalid value for `{{name}}`, must be a follow pattern or equal to `{{{pattern}}}`")  # noqa: E501
jops-wtakase added a commit to jops-wtakase/openapi-generator that referenced this issue Aug 30, 2024
* Fix a bug which generates SyntaxWarning
jops-wtakase added a commit to jops-wtakase/openapi-generator that referenced this issue Aug 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant