Skip to content

Properties that are both nullable & required use Optional without importing it #180

Closed
@dtkav

Description

@dtkav

Describe the bug
If a property is both nullable and required, It will be defined as Optional, but Optional won't be important leading to a warning during code generation:
test-api-client/test_api_client/models/cat.py:11:9: F821 undefined name 'Optional'

To Reproduce
Steps to reproduce the behavior:

+ rm -rf test-api-client
+ openapi-python-client generate --path test.yaml
Generating test-api-client
+ autoflake --in-place --remove-all-unused-imports --remove-unused-variables --expand-star-imports -r test-api-client/
+ isort test-api-client/
+ black test-api-client/
reformatted /Users/daniel/openapi-specs-upstream-repros/test-api-client/test_api_client/models/__init__.py
All done! ✨ 🍰 ✨
1 file reformatted, 7 files left unchanged.
+ flake8 --ignore=E722,E501 test-api-client/
test-api-client/test_api_client/models/cat.py:11:9: F821 undefined name 'Optional'

+ cat test-api-client/test_api_client/models/cat.py
from __future__ import annotations

from dataclasses import dataclass
from typing import Any, Dict


@dataclass
class Cat:
    """  """

    id: Optional[str]

    def to_dict(self) -> Dict[str, Any]:
        id = self.id

        return {
            "id": id,
        }

    @staticmethod
    def from_dict(d: Dict[str, Any]) -> Cat:
        id = d["id"]

        return Cat(
            id=id,
        )

Expected behavior
Optional should be imported if it is used in a file.

OpenAPI Spec File
https://gist.github.com/dtkav/1e40db511a80ca4a545fa4519a8d9adf

Desktop (please complete the following information):

  • OS: macOS 10.15.6
  • Python Version: 3.8.0
  • openapi-python-client version 0.6.0-alpha

Additional context
Potentially fixed by #177

Metadata

Metadata

Assignees

No one assigned

    Labels

    🐞bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions