TypeError: Rectangle.__init__() takes 4 positional arguments but 5 were given

Hi Matplotlib,
def kareciz(self, xy: tuple, width: float, height: float, *args, z: float = 0, **kwargs):
kare = Rectangle(xy, width, height, args, kwargs) #5 positional arguments: Matplotlib: working (-3.7.3), not working (3.8.0)
self.main_ax.add_patch(kare)
return kare
TypeError: Rectangle.init() takes 4 positional arguments but 5 were given

def kareciz(self, xy: tuple, width: float, height: float, *args, z: float = 0, **kwargs):
kare = Rectangle(xy, width, height, **kwargs) #4 positional arguments: working (3.8.0) but positions and shapes are incorrect.
self.main_ax.add_patch(kare)
return kare

Is this due to 3.8? Is there a solution that can be done just in the code here?

I found the solution:

def kareciz(self, xy: tuple, width, height, angle=0, *args, z: float = 0, **kwargs):
kare = Rectangle(xy, width, height, angle=angle, **kwargs) # When there is no angle, position and shape information is corrupted.
self.main_ax.add_patch(kare)
return kare

Your first example:

should have thrown a deprecation warning at v3.7.3. Did it not?

1 Like

I received a warning for the first time in version 3.8.0rc1. After many attempts, my incorrect outputs were corrected when I typed the angle data explicitly (position and shape information was incorrect). Normally it should have given a warning, but I didn’t see it.

It sounds like args contains a single positional value in your call to
kareciz(). So Rectangle(xy, width, height, *args, **kwargs) passes 4
positional arguments to Rectangle(), which in turn passes 5 (self,
xy, width, height and the value fom args) to
Rectangle.__init__.

I do not know, but looking at the docs:
https://matplotlib.org/stable/api/_as_gen/matplotlib.patches.Rectangle.html

it appears that angle etc are keyword only. Maybe they were optional
but positional in 3.7.3? i.e. Rectangle.__init__ looked like:

 def __init__(self, xy, width, height, angle=0.0, rotation_point='xy', **kwargs):

i.e. without the bare * marker indicating an end to the positional
arguments. Without that marker a call like:

 Rectangle( (3,4), 5, 6, 7)

would pass 7 for the angle argument. In 3.8 the angle argument
must be supplied by name.

Cheers,
Cameron Simpson cs@cskk.id.au

···

On 25Sep2023 12:43, Mehmet Keçeci via Matplotlib nobody@discourse.matplotlib.org wrote:

def kareciz(self, xy: tuple, width: float, height: float, *args, z: float = 0, **kwargs):
kare = Rectangle(xy, width, height, args, kwargs) #5 positional arguments: Matplotlib: working (-3.7.3), not working (3.8.0)
self.main_ax.add_patch(kare)
return kare
TypeError: Rectangle.init() takes 4 positional arguments but 5 were given

def kareciz(self, xy: tuple, width: float, height: float, *args, z: float = 0, **kwargs):
kare = Rectangle(xy, width, height, **kwargs) #4 positional arguments: working (3.8.0) but positions and shapes are incorrect.
self.main_ax.add_patch(kare)
return kare

Is this due to 3.8? Is there a solution that can be done just in the
code here?

1 Like

Hi Cameron,

def _draw_rectangle(self, xy: tuple, width: float, height: float, angle: float=0.0, rotation_point='xy', *, z: float = 0, **kwargs):
    #SyntaxError: * argument may appear only once
    #TypeError: Template2D._draw_rectangle() takes 4 positional arguments but 5 positional arguments (and 1 keyword-only argument) were given
    #AttributeError: Rectangle.set() got an unexpected keyword argument 'z'
    rect = Rectangle(xy, width, height, angle=angle, rotation_point=rotation_point, **kwargs) 
    # Without angle, position and shape information is corrupted: class matplotlib.patches.Rectangle(xy, width, height, *, angle=0.0, rotation_point='xy', **kwargs) 
    #https://matplotlib.org/stable/api/_as_gen/matplotlib.patches.Rectangle.html
    self.main_ax.add_patch(rect)
    return rect   

It works for me in this order too (without * or *args or both, as long as there is an angle, but since * is preferred for new codes, it would make more sense to use *). But it doesn’t work without z because it calls it somewhere else. It works correctly with or without rotation.