Skip to content

Newbie question about how to deal with builtins. #7931

Closed
@YangTianz

Description

@YangTianz
  • Are you reporting a bug, or opening a feature request?
    It's a newbie question.

  • Please insert below the code you are checking with mypy

foo.py

class Person(object):
    def __init__(self, name, age, is_male):
        # type: (str, int, bool) -> None
        self.name = name
        self.age = age
        self.is_male = is_male

    def greeting(self):
        # type: () -> None
        print "Hello, " + self.name

def init():
    __builtins__["Person"] = Person
bar.py

from foo import init

init()
alice = Person("Alice", 99, False)
alice.greeting()
  • What is the actual behavior/output?
$ mypy --py2 bar.py foo.py
bar.py:7: error: Name 'Person' is not defined
Found 1 error in 1 file (checked 2 source files)

I defined a class named Person and added it into __builtins__, but mypy could not recognize it. I wonder what I can do to make it work. Thank you.

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