I am using some classes that derived from a parent class (Widget
); among the children, some have certain attributes (posx
and posy
) but some don't.
import enum
from dataclasses import dataclass
from typing import List
class Color(enum.IntEnum):
GLOWING_IN_THE_DARK = enum.auto()
BROWN_WITH_RAINBOW_DOTS = enum.auto()
@dataclass
class Widget:
"""Generic class for widget"""
@dataclass
class Rectangle(Widget):
"""A Color Rectangle"""
posx: int
posy: int
width: int = 500
height: int = 200
color: Color = Color.BROWN_WITH_RAINBOW_DOTS
@dataclass
class Group(Widget):
children: List[Widget]
@dataclass
class Button(Widget):
"""A clickable button"""
posx: int
posy: int
width: int = 200
height: int = 100
label: str = "some label"
Even after doing some filtering with only widgets with these attributes, mypy
is not able to recognize that they should have.
Is there a way to indicate to mypy
that we have an object with a given attribute?
For example, the following function and call:
def some_function_that_does_something(widgets: List[Widget]):
"""A useful docstring that says what the function does"""
widgets_with_pos = [w for w in widgets if hasattr(w, "posx") and hasattr(w, "posy")]
if not widgets_with_pos:
raise AttributeError(f"No widget with position found among list {widgets}")
first_widget = widgets_with_pos[0]
pos_x = first_widget.posx
pos_y = first_widget.posy
print(f"Widget {first_widget} with position: {(pos_x, pos_y)}")
some_widgets = [Group([Rectangle(0, 0)]), Button(10, 10, label="A button")]
some_function_that_does_something(some_widgets)
would return a result as expected: Widget Button(posx=10, posy=10, width=200, height=100, label='A button') with position: (10, 10)
But mypy
would complain:
__check_pos_and_mypy.py:53: error: "Widget" has no attribute "posx"
pos_x = first_widget.posx
^
__check_pos_and_mypy.py:54: error: "Widget" has no attribute "posy"
pos_y = first_widget.posy
^
Found 2 errors in 1 file (checked 1 source file)
How to do?
Maybe, one way could be to change the design of the classes:
- Subclass of
Widget
with the position (e.g.WidgetWithPos
) Rectangle
andButton
would derive from this class- We indicate in our function:
widget_with_pos: List[WidgetWithPos] = ...
... however, I cannot change the original design of the classes and mypy
might still complain with something like:
List comprehension has incompatible type List[Widget]; expected List[WidgetWithPos]
Of course, we could put a bunch of # type:ignore
but that will clutter the code and I am sure there is a smarter way ;)
Thanks!