swagger-api / swagger-api/swagger-codegen

[Python] Shadows built-in name 'range'

Open
#11,296 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Run the reported swagger-codegen generate command against the linked eBay OpenAPI document, then inspect the generated get_item_feed signature and search for other uses of the range parameter. Trace the Python generator entry point responsible for parameter naming. Done means generated Python avoids shadowing the built-in while still handling reserved-word renaming correctly.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.