Skip to content

stubgen does not seem to retain member variable type hint #5616

Closed

Description

@alexyiu875

Please provide more information to help us understand the issue:

  • Are you reporting a bug, or opening a feature request?
    A bug (potentially) in Stubgen

  • Please insert below the code you are checking with mypy,
    or a mock-up repro if the source is private. We would appreciate
    if you try to simplify your case to a minimal repro.
    I am using stubgen to generate ".pyi" file.
    Original Python Source Code for this "Car" class is:

class Car:
    def __init__(self, manufacturer: str, make: str, mpg: float) -> None:
        self.manufacturer: str = manufacturer
        self.make: str = make
        self.mpg: float = mpg
  • What is the actual behavior/output?
class Car:
    manufacturer: Any = ...
    make: Any = ...
    mpg: Any = ...
    def __init__(self, manufacturer: str, make: str, mpg: float) -> None: ...
  • What is the behavior/output you expect?
    I expect the type hint of those member variables would be retained instead of using "Any".
    e.g.
class Car:
    manufacturer: str = ...
    make: str = ...
    mpg: float = ...
    def __init__(self, manufacturer: str, make: str, mpg: float) -> None: ...
  • What are the versions of mypy and Python you are using?
    Do you see the same issue after installing mypy from Git master?
    mypy 0.620
    python 3.7.0
    (I have not tried out mypy from Git master)

  • What are the mypy flags you are using? (For example --strict-optional)
    N/A

  • If mypy crashed with a traceback, please paste
    the full traceback below.
    N/A

If there is a workaround to code this "Car" class in a different variant, which retains TypeHint during Stubgen, please let me know also.

Thanks.

Activity

  1. JukkaL commented on Sep 17, 2018

    @JukkaL
    Collaborator

    This has not been implemented -- stubgen is currently only useful for unannotated modules, unfortunately. Closing as duplicate of #3475.

  2. ilevkivskyi commented on Sep 17, 2018

    @ilevkivskyi
    Member

    @JukkaL
    Actually this (preserving explicit annotations) was implemented in #3169, but it looks like the implementation has an inconsistency (bug?): it works correctly when an attribute is declared in the class body, but doesn't work if it was declared in __init__.

    Should we reopen this for tracking preserving explicit annotations, and keep #3475 for inferred, or you want to have the latter for both?

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