为什么mypy说我的论点太多了

时间:2017-11-26 05:17:53

标签: python mypy

我有一个类,它有一个创建类实例的方法。

    class TitleScene(SceneBase):
    def __init__(self):
        SceneBase.__init__(self)
        #Create buttons and font instances here 
        #self.play_button = Button("Play", (60, 30),lambda:self.SwitchToScene(DifficultyScene())) 
        self.play_button = Button("Play", (60, 30), self.switch_to_difficulty_scene)

        #self.SwitchToScene(DifficultyScene()))
        self.my_font = pygame.font.SysFont("Moyko", 50)

    def ProcessInput(self, events, pressed_keys):
        for event in events:
            if event.type == pygame.KEYDOWN and event.key == pygame.K_RETURN:
                # Move to the next scene when the user pressed Enter 
                self.SwitchToScene(DifficultyScene())

            if event.type == pygame.KEYUP:
                print("You are hitting up!") 
                print(self.next)

            if event.type == pygame.MOUSEBUTTONDOWN:
                mousebuttondown(self.play_button)

    def Update(self):
        pass

    def Render(self, screen):
        # For the sake of brevity, the title scene is a blank red screen 
        screen.fill((255, 0, 0))

        #Just Draw the Text Here 
        #myfont = pygame.font.SysFont(("Moyko"), 50)
        textImage = self.my_font.render("Anime Pong", True, (0, 255, 0))
        screen.blit(textImage, (100,100))

        #Just draw the button here 
        self.play_button.draw()

    def switch_to_difficulty_scene(self):
        self.SwitchToScene(DifficultyScene())


def my_great_function():
    print("Great! " * 5)


def my_fantastic_function():
    print("Fantastic! " * 4)

def change_to_easymode():
    pongtry4.game_mode = "Easy"
    print(pongtry4.game_mode)

def change_to_hardmode():
    pongtry4.game_mode = "Hard"
    print(pongtry4.game_mode)



class DifficultyScene(SceneBase):
    def __init__(self):
        SceneBase.__init__(self)

        self.easy_button = Button("Easy", (60, 30), change_to_easymode)
        self.hard_button = Button("Hard", (120, 60), change_to_hardmode)

        self.my_font = pygame.font.SysFont("Arial", 50)


    def ProcessInput(self, events, pressed_keys):

        for event in events:
            if event.type == pygame.KEYDOWN and event.key == pygame.K_RETURN:
                # Move to the next scene when the user pressed Enter 
                self.SwitchToScene(GameScene())

            if event.type == pygame.KEYUP:
                print("You are hitting up!") 
                print(self.next)

            if event.type == pygame.MOUSEBUTTONDOWN:
                mousebuttondown(self.easy_button)
                mousebuttondown(self.hard_button)

    def Update(self):

        if pongtry4.game_mode == "Easy":
            textImage = self.my_font.render("This is Easy Mode", True, BLACK)
            screen.blit(textImage, (500, 200))
        if pongtry4.game_mode == "Hard":
            textImage = self.my_font.render("This is Hard Mode", True, BLACK)
            screen.blit(textImage, (500, 200))

    def Render(self, screen):
        # The game scene is just a blank blue screen 
        screen.fill((255, 0, 255))

        self.easy_button.draw()
        self.hard_button.draw()


        textImage = self.my_font.render("Choose a Difficulty Mode!", True, (0, 255, 0))
        screen.blit(textImage, (100,100))


class GameScene(SceneBase):
    def __init__(self):
        SceneBase.__init__(self)



    def ProcessInput(self, events, pressed_keys):
        for event in events:
            if event.type == pygame.KEYDOWN and event.key == pygame.K_RETURN:
                # Move to the next scene when the user pressed Enter 
                self.SwitchToScene(GameOverScene())

            if event.type == pygame.KEYUP:
                print("You are hitting up!") 
                print(self.next)


    def Update(self):
        pongtry4.main()

        if pongtry4.gameover_state == True:
            self.SwitchToScene(GameOverScene())

    def Render(self, screen):
        # The game scene is just a blank blue screen 
        #screen.fill((0, 0, 255))
        pass 



class GameOverScene(SceneBase):
    def __init__(self):
        SceneBase.__init__(self)

        self.restart_button = Button("Try Again?", (60, 30), change_to_easymode)



    def ProcessInput(self, events, pressed_keys):
        for event in events:
            if event.type == pygame.KEYDOWN and event.key == pygame.K_RETURN:
                # Move to the next scene when the user pressed Enter 
                self.SwitchToScene(DifficultyScene())

            if event.type == pygame.KEYUP:
                print("You are hitting up!") 
                print(self.next)

            if event.type == pygame.MOUSEBUTTONDOWN:
                mousebuttondown(self.play_button)

    def Update(self):
        pass

    def Render(self, screen):
        # The game scene is just a blank blue screen 
        screen.fill(GREY)

        self.restart_button.draw()

run_game(800, 400, 60, TitleScene())

当我对它运行mypy时,它会显示class Bar: @classmethod def create(cls, foo: int): return cls(foo) def __init__(self, foo: int) -> None: pass

对我来说这似乎是一个错误,因为这很好用

mypytest.py:4: error: Too many arguments for "Bar"

我不明白为什么定义参数类型的类方法应该破坏实例的创建。我错过了什么吗?

1 个答案:

答案 0 :(得分:2)

答案很简单,只需先放置__init__方法即可。例如,这很好用:

class Bar:
    def __init__(self, foo: int) -> None:
        pass

    @classmethod
    def create(cls, foo: int):
        return cls(foo)

由于某些技术原因,如果__init__(或__new__)不是类定义中的第一个方法,mypy目前在某些极端情况下会出现意外行为。我想我已经看到了类似的问题,但在mypy跟踪器上找不到问题。