Skip to content

TypeError: Mode.__init__() got an unexpected keyword argument 'experimental_string_processing' #406

Closed

Description

@angelgonzalbez

Using python 3.11.7 or 3.10.7 in pyenv venv in macOS

(openapi-srv) ➜ openapi-srv fastapi-codegen --input api.yaml --output app3
╭─────────────────────────────── Traceback (most recent call last) ────────────────────────────────╮
│ /Users/myuser/.pyenv/versions/3.11.7/envs/openapi-srv/lib/python3.11/site-packages/fastapi_cod │
│ e_generator/main.py:86 in main │
│ │
│ 83 │ │ │ generate_routers=generate_routers, │
│ 84 │ │ │ specify_tags=specify_tags, │
│ 85 │ │ ) │
│ ❱ 86 │ return generate_code( │
│ 87 │ │ input_name, │
│ 88 │ │ input_text, │
│ 89 │ │ encoding, │
│ │
│ ╭─────────────────────────────────────────── locals ───────────────────────────────────────────╮ │
│ │ custom_visitors = [] │ │
│ │ disable_timestamp = False │ │
│ │ encoding = 'utf-8' │ │
│ │ enum_field_as_literal = None │ │
│ │ f = <io.TextIOWrapper name='api.yaml' mode='r' encoding='utf-8'> │ │
│ │ generate_routers = False │ │
│ │ input_file = 'api.yaml' │ │
│ │ input_name = 'api.yaml' │ │
│ │ input_text = 'openapi: "3.0.0"\ninfo:\n version: 1.0.0\n title: Swagger │ │
│ │ Petstore\n license:\n '+3415 │ │
│ │ model_file = None │ │
│ │ model_path = PosixPath('models.py') │ │
│ │ output_dir = PosixPath('app3') │ │
│ │ specify_tags = None │ │
│ │ template_dir = None │ │
│ ╰──────────────────────────────────────────────────────────────────────────────────────────────╯ │
│ │
│ /Users/myuser/.pyenv/versions/3.11.7/envs/openapi-srv/lib/python3.11/site-packages/fastapi_cod │
│ e_generator/main.py:138 in generate_code │
│ │
│ 135 │ else: │
│ 136 │ │ parser = OpenAPIParser(input_text) │
│ 137 │ with chdir(output_dir): │
│ ❱ 138 │ │ models = parser.parse() │
│ 139 │ output = output_dir / model_path │
│ 140 │ if not models: │
│ 141 │ │ # if no models (schemas), just generate an empty model file. │
│ │
│ ╭─────────────────────────────────────────── locals ───────────────────────────────────────────╮ │
│ │ custom_visitors = [] │ │
│ │ disable_timestamp = False │ │
│ │ encoding = 'utf-8' │ │
│ │ enum_field_as_literal = None │ │
│ │ generate_routers = False │ │
│ │ input_name = 'api.yaml' │ │
│ │ input_text = 'openapi: "3.0.0"\ninfo:\n version: 1.0.0\n title: Swagger │ │
│ │ Petstore\n license:\n '+3415 │ │
│ │ model_path = PosixPath('models.py') │ │
│ │ output_dir = PosixPath('app3') │ │
│ │ parser = <fastapi_code_generator.parser.OpenAPIParser object at 0x10366b150> │ │
│ │ specify_tags = None │ │
│ │ template_dir = PosixPath('/Users/myuser/.pyenv/versions/3.11.7/envs/openapi-srv/… │ │
│ ╰──────────────────────────────────────────────────────────────────────────────────────────────╯ │
│ │
│ /Users/myuser/.pyenv/versions/3.11.7/envs/openapi-srv/lib/python3.11/site-packages/datamodel_c │
│ ode_generator/parser/base.py:884 in parse │
│ │
│ 881 │ │ │ │ self.imports.append(IMPORT_ANNOTATIONS) │
│ 882 │ │ │
│ 883 │ │ if format
: │
│ ❱ 884 │ │ │ code_formatter: Optional[CodeFormatter] = CodeFormatter( │
│ 885 │ │ │ │ self.target_python_version, │
│ 886 │ │ │ │ settings_path, │
│ 887 │ │ │ │ self.wrap_string_literal, │
│ │
│ ╭────────────────────────────────────── locals ───────────────────────────────────────╮ │
│ │ format_ = True │ │
│ │ self = <fastapi_code_generator.parser.OpenAPIParser object at 0x10366b150> │ │
│ │ settings_path = None │ │
│ │ with_import = True │ │
│ ╰─────────────────────────────────────────────────────────────────────────────────────╯ │
│ │
│ /Users/myuser/.pyenv/versions/3.11.7/envs/openapi-srv/lib/python3.11/site-packages/datamodel_c │
│ ode_generator/format.py:108 in init │
│ │
│ 105 │ │ if TYPE_CHECKING: │
│ 106 │ │ │ self.black_mode: black.FileMode │
│ 107 │ │ else: │
│ ❱ 108 │ │ │ self.black_mode = black.FileMode( │
│ 109 │ │ │ │ target_versions={BLACK_PYTHON_VERSION[python_version]}, │
│ 110 │ │ │ │ line_length=config.get("line-length", black.DEFAULT_LINE_LENGTH), │
│ 111 │ │ │ │ string_normalization=not skip_string_normalization │
│ │
│ ╭─────────────────────────────────────────── locals ───────────────────────────────────────────╮ │
│ │ black_kwargs = {'experimental_string_processing': False} │ │
│ │ config = {} │ │
│ │ experimental_string_processing = False │ │
│ │ path = PosixPath('/Users/myuser/PycharmProjects/openapi-srv/pyp… │ │
│ │ pyproject_toml = { │ │
│ │ │ 'tool': { │ │
│ │ │ │ 'poetry': { │ │
│ │ │ │ │ 'name': 'openapi-srv', │ │
│ │ │ │ │ 'version': '0.1.0', │ │
│ │ │ │ │ 'description': '', │ │
│ │ │ │ │ 'authors': ['Your Name you@example.com'], │ │
│ │ │ │ │ 'readme': 'README.md', │ │
│ │ │ │ │ 'dependencies': { │ │
│ │ │ │ │ │ 'python': '^3.11', │ │
│ │ │ │ │ │ 'fastapi-code-generator': '^0.4.4' │ │
│ │ │ │ │ } │ │
│ │ │ │ } │ │
│ │ │ }, │ │
│ │ │ 'build-system': { │ │
│ │ │ │ 'requires': ['poetry-core'], │ │
│ │ │ │ 'build-backend': 'poetry.core.masonry.api' │ │
│ │ │ } │ │
│ │ } │ │
│ │ python_version = <PythonVersion.PY_37: '3.7'> │ │
│ │ root = PosixPath('/Users/myuser/PycharmProjects/openapi-srv') │ │
│ │ self = <datamodel_code_generator.format.CodeFormatter object at │ │
│ │ 0x10398aa90> │ │
│ │ settings_path = PosixPath('/Users/myuser/PycharmProjects/openapi-srv/app… │ │
│ │ skip_string_normalization = True │ │
│ │ value = '/Users/myuser/PycharmProjects/openapi-srv/pyproject.tom… │ │
│ │ wrap_string_literal = False │ │
│ ╰──────────────────────────────────────────────────────────────────────────────────────────────╯ │
╰──────────────────────────────────────────────────────────────────────────────────────────────────╯
TypeError: Mode.init() got an unexpected keyword argument 'experimental_string_processing'

api.yaml content:
openapi: "3.0.0"
info:
version: 1.0.0
title: Swagger Petstore
license:
name: MIT
servers:

  • url: http://petstore.swagger.io/v1
    paths:
    /pets:
    get:
    summary: List all pets
    operationId: listPets
    tags:
    - pets
    parameters:
    - name: limit
    in: query
    description: How many items to return at one time (max 100)
    required: false
    schema:
    type: integer
    format: int32
    responses:
    '200':
    description: A paged array of pets
    headers:
    x-next:
    description: A link to the next page of responses
    schema:
    type: string
    content:
    application/json:
    schema:
    $ref: "#/components/schemas/Pets"
    default:
    description: unexpected error
    content:
    application/json:
    schema:
    $ref: "#/components/schemas/Error"
    x-amazon-apigateway-integration:
    uri:
    Fn::Sub: arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${PythonVersionFunction.Arn}/invocations
    passthroughBehavior: when_no_templates
    httpMethod: POST
    type: aws_proxy
    post:
    summary: Create a pet
    operationId: createPets
    tags:
    - pets
    responses:
    '201':
    description: Null response
    default:
    description: unexpected error
    content:
    application/json:
    schema:
    $ref: "#/components/schemas/Error"
    x-amazon-apigateway-integration:
    uri:
    Fn::Sub: arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${PythonVersionFunction.Arn}/invocations
    passthroughBehavior: when_no_templates
    httpMethod: POST
    type: aws_proxy
    /pets/{petId}:
    get:
    summary: Info for a specific pet
    operationId: showPetById
    tags:
    - pets
    parameters:
    - name: petId
    in: path
    required: true
    description: The id of the pet to retrieve
    schema:
    type: string
    responses:
    '200':
    description: Expected response to a valid request
    content:
    application/json:
    schema:
    $ref: "#/components/schemas/Pets"
    default:
    description: unexpected error
    content:
    application/json:
    schema:
    $ref: "#/components/schemas/Error"
    x-amazon-apigateway-integration:
    uri:
    Fn::Sub: arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${PythonVersionFunction.Arn}/invocations
    passthroughBehavior: when_no_templates
    httpMethod: POST
    type: aws_proxy
    components:
    schemas:
    Pet:
    required:
    - id
    - name
    properties:
    id:
    type: integer
    format: int64
    name:
    type: string
    tag:
    type: string
    Pets:
    type: array
    description: list of pet
    items:
    $ref: "#/components/schemas/Pet"
    Error:
    required:
    - code
    - message
    properties:
    code:
    type: integer
    format: int32
    message:
    type: string

Activity

  1. r-whites commented on Feb 13, 2024

    @r-whites

    Hey! We have encountered the same issue in my team recently. It seems that the package datamodel-code-generator brings in a version of black that displays that error. We solved the issue by forcing our local version of black to 23.3.0.

  2. nnashiki commented on Feb 20, 2024

    @nnashiki

    It worked as described with Docker.
    thank you. @r-whites

    FROM python:3.11-slim
    
    RUN pip install fastapi-code-generator black==23.3.0
    
    WORKDIR /local
    
    ENTRYPOINT ["fastapi-codegen"]
    $ docker build -t fastapi-code-generator-container .
    $ docker run --rm -v "${PWD}:/local" fastapi-code-generator-container --input /local/openapi.yml --output app
    
  3. DavidRV00 commented on Feb 25, 2024

    @DavidRV00

    Hello, I'm forcing black to version 23.3.0 as described above, but I'm still getting exactly the same error. I've installed both black and fastapi-code-generator using pipx, if that matters at all.

    Anybody else still running into this, or have any ideas?

  4. sefasenturk95 commented on Mar 13, 2024

    @sefasenturk95

    I had this same error, downgrading to the black version as suggested here using poetry worked for me.

  5. Ambujinee12 commented on Mar 22, 2024

    @Ambujinee12

    I was also facing the same issue, also downgrading black to 23.3.0 helped me here.

  6. koxudaxi commented on May 31, 2026

    @koxudaxi
    Owner

    Resolved by #596.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions