{"record":{"id":"c2d3816dbaf3614f","repo":"geekcomputers/Python","slug":"the-background-argument-must-be-an-instance-of-bac","errorCode":null,"errorMessage":"The background argument must be an instance of Background.","messagePattern":"The background argument must be an instance of Background\\.","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"Flappy Bird - created with tkinter/Bird.py","lineNumber":35,"sourceCode":"    __times_skipped = 0\n    __running = False\n\n    decends = 0.00390625\n    climbsUp = 0.0911458333\n\n    def __init__(\n        self,\n        background,\n        gameover_function,\n        *screen_geometry,\n        fp=\"bird.png\",\n        event=\"<Up>\",\n        descend_speed=5,\n    ):\n        # Verifica se \"background\" é uma instância de Background e se o \"gamerover_method\" é chamável\n\n        if not isinstance(background, Background):\n            raise TypeError(\n                \"The background argument must be an instance of Background.\"\n            )\n        if not callable(gameover_function):\n            raise TypeError(\"The gameover_method argument must be a callable object.\")\n\n        # Instância os parâmetros\n        self.__canvas = background\n        self.image_path = fp\n        self.__descend_speed = descend_speed\n        self.gameover_method = gameover_function\n\n        # Recebe a largura e altura do background\n        self.__width = screen_geometry[0]\n        self.__height = screen_geometry[1]\n\n        # Define a decida e subida do pássaro com base na altura do background\n        self.decends *= self.__height\n        self.decends = int(self.decends + 0.5)","sourceCodeStart":17,"sourceCodeEnd":53,"githubUrl":"https://github.com/geekcomputers/Python/blob/40f4cd2652d75ef8e49d76e5c4d431d458712719/Flappy Bird - created with tkinter/Bird.py#L17-L53","documentation":"Bird's constructor requires its background argument to be an instance of Background because the bird draws and animates itself on that background's canvas. Passing any other object (or None) raises this TypeError before any state is set up.","triggerScenarios":"Constructing Bird with a Canvas, Tk root, or None instead of a Background instance; creating Bird before the Background object exists.","commonSituations":"Initialization-order bugs where Bird is instantiated before Background; refactors that replace the Background class with a plain canvas; tests using stubs.","solutions":["Create the Background instance first and pass it to Bird(background=bg)","Fix import cycles that caused a Background module to resolve to a different class","In tests, use unittest.mock.patch on Background or create real lightweight instances"],"exampleFix":"# before\nbird = Bird(canvas, game_over_fn)\n# after\nbg = Background(tk_root)\nbird = Bird(bg, game_over_fn)","handlingStrategy":"type-guard","validationCode":"from Background import Background\nif not isinstance(bg, Background):\n    raise TypeError('bg must be Background')","typeGuard":"def is_background(obj) -> bool:\n    from Background import Background\n    return isinstance(obj, Background)","tryCatchPattern":"try:\n    bird = Bird(bg, game_over)\nexcept TypeError as e:\n    print('Bird argument validation failed:', e)","preventionTips":["Instantiate Background before Bird in the startup sequence","Pass the same Background instance to Bird and Tubes","Avoid replacing Background with a raw Canvas in refactors"],"tags":["tkinter","typeerror","constructor-validation","flappy-bird"],"backgroundTag":"wrong-argument-type","analyzedSha":"40f4cd2652d75ef8e49d76e5c4d431d458712719","analyzedAt":"2026-08-27T11:12:20.313Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}