fastapi icon indicating copy to clipboard operation
fastapi copied to clipboard

"ValueError: 'not' is not a valid parameter name" on python 3.11

Open koviubi56 opened this issue 2 years ago • 4 comments

First Check

  • [X] I added a very descriptive title to this issue.
  • [X] I used the GitHub search to find a similar issue and didn't find it.
  • [X] I searched the FastAPI documentation, with the integrated search.
  • [X] I already searched in Google "How to X in FastAPI" and didn't find any information.
  • [X] I already read and followed all the tutorial in the docs and didn't find an answer.
  • [X] I already checked if it is not related to FastAPI but to Pydantic.
  • [X] I already checked if it is not related to FastAPI but to Swagger UI.
  • [X] I already checked if it is not related to FastAPI but to ReDoc.

Commit to Help

  • [X] I commit to help with one of those options 👆

Example Code

import fastapi

app = fastapi.FastAPI()


@app.get("/")
def index():
    return {"message": "Hello World"}

Description

In Python 3.11 (Python 3.11.0b3 (main, Jun 1 2022, 13:29:14) [MSC v.1932 64 bit (AMD64)]), importing fastapi raises an exception: ValueError: 'not' is not a valid parameter name. (Running the file in Python 3.10, 3.9, and 3.8 works as expected)

Traceback

Running the file

Traceback (most recent call last):
  File "C:\Users\Tibi\Documents\example.py", line 1, in 
    import fastapi
    ^^^^^^^^^^^^^^
  File "C:\Users\Tibi\AppData\Local\Programs\Python\Python311\Lib\site-packages\fastapi\__init__.py", line 7, in 
    from .applications import FastAPI as FastAPI
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Tibi\AppData\Local\Programs\Python\Python311\Lib\site-packages\fastapi\applications.py", line 15, in 
    from fastapi import routing
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Tibi\AppData\Local\Programs\Python\Python311\Lib\site-packages\fastapi\routing.py", line 23, in 
    from fastapi.dependencies.models import Dependant
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Tibi\AppData\Local\Programs\Python\Python311\Lib\site-packages\fastapi\dependencies\models.py", line 3, in 
    from fastapi.security.base import SecurityBase
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Tibi\AppData\Local\Programs\Python\Python311\Lib\site-packages\fastapi\security\__init__.py", line 1, in 
    from .api_key import APIKeyCookie as APIKeyCookie
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Tibi\AppData\Local\Programs\Python\Python311\Lib\site-packages\fastapi\security\api_key.py", line 3, in 
    from fastapi.openapi.models import APIKey, APIKeyIn
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Tibi\AppData\Local\Programs\Python\Python311\Lib\site-packages\fastapi\openapi\models.py", line 103, in 
    class Schema(BaseModel):
    ^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Tibi\AppData\Local\Programs\Python\Python311\Lib\site-packages\pydantic\main.py", line 292, in __new__
    cls.__signature__ = ClassAttribute('__signature__', generate_model_signature(cls.__init__, fields, config))
                                                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Tibi\AppData\Local\Programs\Python\Python311\Lib\site-packages\pydantic\utils.py", line 258, in generate_model_signature
    merged_params[param_name] = Parameter(
                                ^^^^^^^^^^
  File "C:\Users\Tibi\AppData\Local\Programs\Python\Python311\Lib\inspect.py", line 2715, in __init__
    raise ValueError('{!r} is not a valid parameter name'.format(name))
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ValueError: 'not' is not a valid parameter name

Using uvicorn

Traceback (most recent call last):
  File "", line 198, in _run_module_as_main
  File "", line 88, in _run_code
  File "C:\Users\Tibi\AppData\Local\Programs\Python\Python311\Lib\site-packages\uvicorn\__main__.py", line 4, in 
    uvicorn.main()
    ^^^^^^^^^^^^^^
  File "C:\Users\Tibi\AppData\Local\Programs\Python\Python311\Lib\site-packages\click\core.py", line 1128, in __call__
    return self.main(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Tibi\AppData\Local\Programs\Python\Python311\Lib\site-packages\click\core.py", line 1053, in main
    rv = self.invoke(ctx)
         ^^^^^^^^^^^^^^^^
  File "C:\Users\Tibi\AppData\Local\Programs\Python\Python311\Lib\site-packages\click\core.py", line 1395, in invoke
    return ctx.invoke(self.callback, **ctx.params)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Tibi\AppData\Local\Programs\Python\Python311\Lib\site-packages\click\core.py", line 754, in invoke
    return __callback(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Tibi\AppData\Local\Programs\Python\Python311\Lib\site-packages\uvicorn\main.py", line 437, in main
    run(app, **kwargs)
    ^^^^^^^^^^^^^^^^^^
  File "C:\Users\Tibi\AppData\Local\Programs\Python\Python311\Lib\site-packages\uvicorn\main.py", line 463, in run
    server.run()
    ^^^^^^^^^^^^
  File "C:\Users\Tibi\AppData\Local\Programs\Python\Python311\Lib\site-packages\uvicorn\server.py", line 60, in run
    return asyncio.run(self.serve(sockets=sockets))
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Tibi\AppData\Local\Programs\Python\Python311\Lib\asyncio\runners.py", line 181, in run
    return runner.run(main)
           ^^^^^^^^^^^^^^^^
  File "C:\Users\Tibi\AppData\Local\Programs\Python\Python311\Lib\asyncio\runners.py", line 115, in run
    return self._loop.run_until_complete(task)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Tibi\AppData\Local\Programs\Python\Python311\Lib\asyncio\base_events.py", line 650, in run_until_complete
    return future.result()
           ^^^^^^^^^^^^^^^
  File "C:\Users\Tibi\AppData\Local\Programs\Python\Python311\Lib\site-packages\uvicorn\server.py", line 67, in serve
    config.load()
    ^^^^^^^^^^^^^
  File "C:\Users\Tibi\AppData\Local\Programs\Python\Python311\Lib\site-packages\uvicorn\config.py", line 458, in load
    self.loaded_app = import_from_string(self.app)
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Tibi\AppData\Local\Programs\Python\Python311\Lib\site-packages\uvicorn\importer.py", line 21, in import_from_string
    module = importlib.import_module(module_str)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Tibi\AppData\Local\Programs\Python\Python311\Lib\importlib\__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "", line 1206, in _gcd_import
  File "", line 1178, in _find_and_load
  File "", line 1149, in _find_and_load_unlocked
  File "", line 690, in _load_unlocked
  File "", line 939, in exec_module
  File "", line 241, in _call_with_frames_removed
  File "C:\Users\Tibi\Documents\.\example.py", line 1, in 
    import fastapi
    ^^^^^^^^^^^^^^
  File "C:\Users\Tibi\AppData\Local\Programs\Python\Python311\Lib\site-packages\fastapi\__init__.py", line 7, in 
    from .applications import FastAPI as FastAPI
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Tibi\AppData\Local\Programs\Python\Python311\Lib\site-packages\fastapi\applications.py", line 15, in 
    from fastapi import routing
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Tibi\AppData\Local\Programs\Python\Python311\Lib\site-packages\fastapi\routing.py", line 23, in 
    from fastapi.dependencies.models import Dependant
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Tibi\AppData\Local\Programs\Python\Python311\Lib\site-packages\fastapi\dependencies\models.py", line 3, in 
    from fastapi.security.base import SecurityBase
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Tibi\AppData\Local\Programs\Python\Python311\Lib\site-packages\fastapi\security\__init__.py", line 1, in 
    from .api_key import APIKeyCookie as APIKeyCookie
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Tibi\AppData\Local\Programs\Python\Python311\Lib\site-packages\fastapi\security\api_key.py", line 3, in 
    from fastapi.openapi.models import APIKey, APIKeyIn
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Tibi\AppData\Local\Programs\Python\Python311\Lib\site-packages\fastapi\openapi\models.py", line 103, in 
    class Schema(BaseModel):
    ^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Tibi\AppData\Local\Programs\Python\Python311\Lib\site-packages\pydantic\main.py", line 292, in __new__
    cls.__signature__ = ClassAttribute('__signature__', generate_model_signature(cls.__init__, fields, config))
                                                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Tibi\AppData\Local\Programs\Python\Python311\Lib\site-packages\pydantic\utils.py", line 258, in generate_model_signature
    merged_params[param_name] = Parameter(
                                ^^^^^^^^^^
  File "C:\Users\Tibi\AppData\Local\Programs\Python\Python311\Lib\inspect.py", line 2715, in __init__
    raise ValueError('{!r} is not a valid parameter name'.format(name))
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ValueError: 'not' is not a valid parameter name

The problem seems to be this line:

# File "C:\Users\Tibi\AppData\Local\Programs\Python\Python311\Lib\site-packages\fastapi\openapi\models.py"
class Schema(BaseModel):
    # ...
    #                                              ~~~~~~~~~~~
    not_: Optional["Schema"] = Field(default=None, alias="not")
    # ...

After changing that line to not_: Optional["Schema"] = None, there's another similar exception: ValueError: 'in' is not a valid parameter name, which is because of this:

# File "C:\Users\Tibi\AppData\Local\Programs\Python\Python311\Lib\site-packages\fastapi\openapi\models.py"
class Parameter(ParameterBase):
    name: str
    #                                 ~~~~~~~~~~
    in_: ParameterInType = Field(..., alias="in")

After changing that line to in_: ParameterInType, there's another similar exception: ValueError: 'in' is not a valid parameter name, which is because of this:

# File "C:\Users\Tibi\AppData\Local\Programs\Python\Python311\Lib\site-packages\fastapi\openapi\models.py"
class APIKey(SecurityBase):
    type_ = Field(SecuritySchemeType.apiKey, alias="type")
    #                          ~~~~~~~~~~
    in_: APIKeyIn = Field(..., alias="in")
    name: str

After changing that line to in_: APIKeyIn, the code works as expected.

Operating System

Windows

Operating System Details

No response

FastAPI Version

0.78.0

Python Version

Python 3.11.0b3

Additional Context

If you would like to, I could make a PR that removes these aliases.

koviubi56 avatar Jun 17 '22 11:06 koviubi56

Having the same issue on ARM-64 MacOS with Python 3.11.0b3

Traceback (most recent call last):
  File "/Users/alex/Code/Python/Flashcard/main.py", line 1, in <module>
    from fastapi import FastAPI
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/alex/.pyenv/versions/3.11-dev/envs/notecards/lib/python3.11/site-packages/fastapi/__init__.py", line 7, in <module>
    from .applications import FastAPI as FastAPI
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/alex/.pyenv/versions/3.11-dev/envs/notecards/lib/python3.11/site-packages/fastapi/applications.py", line 15, in <module>
    from fastapi import routing
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/alex/.pyenv/versions/3.11-dev/envs/notecards/lib/python3.11/site-packages/fastapi/routing.py", line 23, in <module>
    from fastapi.dependencies.models import Dependant
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/alex/.pyenv/versions/3.11-dev/envs/notecards/lib/python3.11/site-packages/fastapi/dependencies/models.py", line 3, in <module>
    from fastapi.security.base import SecurityBase
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/alex/.pyenv/versions/3.11-dev/envs/notecards/lib/python3.11/site-packages/fastapi/security/__init__.py", line 1, in <module>
    from .api_key import APIKeyCookie as APIKeyCookie
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/alex/.pyenv/versions/3.11-dev/envs/notecards/lib/python3.11/site-packages/fastapi/security/api_key.py", line 3, in <module>
    from fastapi.openapi.models import APIKey, APIKeyIn
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/alex/.pyenv/versions/3.11-dev/envs/notecards/lib/python3.11/site-packages/fastapi/openapi/models.py", line 103, in <module>
    class Schema(BaseModel):
    ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/alex/.pyenv/versions/3.11-dev/envs/notecards/lib/python3.11/site-packages/pydantic/main.py", line 292, in __new__
    cls.__signature__ = ClassAttribute('__signature__', generate_model_signature(cls.__init__, fields, config))
                                                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/alex/.pyenv/versions/3.11-dev/envs/notecards/lib/python3.11/site-packages/pydantic/utils.py", line 258, in generate_model_signature
    merged_params[param_name] = Parameter(
                                ^^^^^^^^^^
  File "/Users/alex/.pyenv/versions/3.11-dev/lib/python3.11/inspect.py", line 2715, in __init__
    raise ValueError('{!r} is not a valid parameter name'.format(name))
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

alexfromapex avatar Jun 19 '22 20:06 alexfromapex

This seems to be related to the following issue in CPython: https://github.com/python/cpython/issues/92062 The associated PR is: https://github.com/python/cpython/pull/92065 A related issue in Pydantic is: https://github.com/samuelcolvin/pydantic/issues/4011 A related PR in Pydantic is: https://github.com/samuelcolvin/pydantic/pull/4012

musicinmybrain avatar Jun 29 '22 16:06 musicinmybrain

The issue is still present in FastAPI 0.79.0 and Python 3.11.0b4.

musicinmybrain avatar Jul 15 '22 18:07 musicinmybrain

Any thoughts on whether or not simply removing the aliases as described in the original report is an appropriate fix?

musicinmybrain avatar Jul 28 '22 21:07 musicinmybrain

I found that removing the aliases as suggested leads to errors even collecting the test suite, let alone running the tests, since these aliases are used quite heavily.

musicinmybrain avatar Aug 25 '22 13:08 musicinmybrain

The python-fastapi package will be retired from Fedora Linux beginning with Fedora 37 due to this issue.

musicinmybrain avatar Aug 25 '22 13:08 musicinmybrain

This issue has been resolved in v1.10.0a1 of Pydantic.

koviubi56 avatar Aug 27 '22 12:08 koviubi56