Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
dtkav opened this issue Sep 4, 2020 · 1 comment
Labels
🐞bug Something isn't working

Comments

@dtkav
Copy link
Contributor

dtkav commented Sep 4, 2020

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

@dtkav dtkav added the 🐞bug Something isn't working label Sep 4, 2020
@dtkav dtkav changed the title properties that are both nullable & required use Optional without importing it Properties that are both nullable & required use Optional without importing it Sep 4, 2020
@dbanty
Copy link
Collaborator

dbanty commented Sep 4, 2020

Fixed in 0.5.5 and 0.6.0-alpha.2

@dbanty dbanty closed this as completed Sep 4, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🐞bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants