swagger-api / swagger-codegen

swagger-codegen contains a template-driven engine to generate documentation, API clients and server stubs in different languages by parsing your OpenAPI / Swagger definition.
http://swagger.io
Apache License 2.0
16.8k stars 6.02k forks source link

[Python] Shadows built-in name 'range' #11296

Open matecsaj opened 2 years ago

matecsaj commented 2 years ago
Description

Reusing a name in and out of a function is referred to as "name shadowing." Python happens to have already something named 'range' https://docs.python.org/3/library/stdtypes.html?highlight=range#range. Shadowing is not a syntax error, but working around the name conflict makes the code less readable.

Swagger-codegen version

3.0.29 stable release

Swagger declaration file content or url

https://developer.ebay.com/api-docs/master/buy/feed/openapi/3/buy_feed_v1_beta_oas3.json

Command line used for generation

/usr/local/bin/swagger-codegen generate -l python -o buy_feed -DpackageName=buy_feed -i https://developer.ebay.com/api-docs/master/buy/feed/openapi/3/buy_feed_v1_beta_oas3.json

Steps to reproduce
  1. Open a command-line terminal.

  2. Run the following command. If not using MacOS, modify this '/usr/local/bin/' part of the command line per your operating system.

/usr/local/bin/swagger-codegen generate -l python -o buy_feed -DpackageName=buy_feed -i https://developer.ebay.com/api-docs/master/buy/feed/openapi/3/buy_feed_v1_beta_oas3.json

  1. In the files generated, search for a lines containing 'def get_item_feed'. On MacOS or Unix you could run grep. grep -R 'def get_item_feed'

  2. Note that the following line of code contains the parameter name 'range'. def get_item_feed(self, accept, x_ebay_c_marketplace_id, range, feed_scope, category_id, **kwargs): # noqa: E501

Related issues/PRs

Unknown, unable to find any.

Suggest a fix/enhancement

When a parameter name is 'range,' perhaps prefix or suffix it with an underscore '_.' Although not a keyword, 'range' is part of the Python language. The generated Python code would have a syntax error if a parameter name happened to be a reserved word, https://docs.python.org/3/reference/lexical_analysis.html#keywords, so likely the Python generator already does some parameter renaming.

matecsaj commented 7 months ago

I checked the current version of the Swagger code generator, and the issue persists.