question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[BUG] Python generator generating reserved word ("from")

See original GitHub issue

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • What’s the version of OpenAPI Generator used? v4.3.1 (implementation-version: 4.3.1)
  • Have you search for related issues/PRs? Yes
  • What’s the actual output vs expected output? See below
  • [Optional] Bounty to sponsor the fix (example)
Description

When using the python generator (not the experimental one) the provided spec generates a python test that uses a reserved word as a variable name. This causes python to blow up.

The spec has to have this property: A python reserved word is used as a nested property in a schema (FailureStats -> FailureStat -> from). From is a reserved word in python but is not escaped because it’s nested. In the provided sample it ends up being generated in a test file:

 def make_instance(self, include_optional):
        """Test FailureStats
            include_option is a boolean, when False only required
            params are included, when True both required and
            optional params are included """
        # model = openapi_client.models.failure_stats.FailureStats()  # noqa: E501
        if include_optional :
            return FailureStats(
                version = 56, 
                status = '0', 
                code = 56, 
                offset = 56, 
                count = 56, 
                failure_stats = [
                    openapi_client.models.failure_stat.FailureStat(
                        samples = 56, 
                        subject = '0', 
                        from = '0',  # HERE!
                        domain = '0', 
                        first_occurrence = datetime.datetime.strptime('2013-10-20 19:20:30.00', '%Y-%m-%d %H:%M:%S.%f'), 
                        last_occurrence = datetime.datetime.strptime('2013-10-20 19:20:30.00', '%Y-%m-%d %H:%M:%S.%f'), 
                        ip_count = 56, 
                        links = [
                            '0'
                            ], )
                    ]
            )
        else :
            return FailureStats(
        )

openapi-generator version

v4.3.1 (implementation-version: 4.3.1). Does not appear to be a regression that I can see.

OpenAPI declaration file content or url
Command line used for generation

java -jar modules/openapi-generator-cli/target/openapi-generator-cli.jar generate -i spec3.yaml -g python -o ./output/

Steps to reproduce

Download openapi-generator-cli.jar from github. java -jar modules/openapi-generator-cli/target/openapi-generator-cli.jar generate -i spec3.yaml -g python -o ./output/ pytest ./output/test/

Related issues/PRs

I couldnt find any that seemed to mimic this issue for python.

Suggest a fix

In PythonClientCodegen.java. On line 872. Just add the following:

if (isReservedWord(propname)) {
     propname = escapeReservedWord(propname);
}

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
indydcarrcommented, Mar 10, 2021

Having the same problem with a nested “self” unfortunately.

TypeError: wrapped_init() got multiple values for argument 'self'.

Tested with v5.0.0-beta3 and 4.3.1.

I have the same exact issue, were you able to find a past version that this was resolved in?

2reactions
0xDAFEcommented, Dec 8, 2020

Having the same problem with a nested “self” unfortunately.

TypeError: wrapped_init() got multiple values for argument 'self'.

Tested with v5.0.0-beta3 and 4.3.1.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Weird Bug in Python Generator - Stack Overflow
The problem seems to arise when I convert the generator to a list in line 80. If I put back the commented-out lines...
Read more >
How to Use Generators and yield in Python
In this step-by-step tutorial, you'll learn about generators and yielding in Python. You'll create generator functions and generator expressions using ...
Read more >
Python Generator - Programiz
In this tutorial, you'll learn how to create iterations easily using Python generators, how it is different from iterators and normal functions, and...
Read more >
PEP 289 – Generator Expressions
Python Enhancement Proposals (PEPs)
Read more >
random — Generate pseudo-random numbers — Python 3.11 ...
This module implements pseudo-random number generators for various ... (The parameter would be called “lambda”, but that is a reserved word in Python.) ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found