KLayout / klayout

KLayout Main Sources
http://www.klayout.org
GNU General Public License v3.0
782 stars 200 forks source link

errors when adding polygons with 4 points #1651

Closed joamatab closed 6 months ago

joamatab commented 7 months ago

When adding polygons with 4 points we get an error

poly1 = kdb.Polygon(
    [
        [-8, -6],
        [6, 8],
        [7, 17],
        [9, 5],
    ]
)
TypeError: Ambiguous overload variants - multiple method declarations match arguments in Polygon.__init__

@amccaugh @sebastian-goeldi

sebastian-goeldi commented 7 months ago

This works, but it seems the .pyi didn't pick up the new function overload.

>>> import klayout.db as kdb
>>> kdb.Polygon([[0,0],[10,0],[10.5,10]])
(0,0;10,10;10,0)
>>> kdb.DPolygon([[0,0],[10,0],[10.,10]])
(0,0;10,10;10,0)
sebastian-goeldi commented 7 months ago

The issue seems to be if the list has exactly 4 points, then the creation fails

>>> poly1=kf.kdb.DPolygon([(0,0),[-10,0],[10.,10],[10,0]])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: Ambiguous overload variants - multiple method declarations match arguments in DPolygon.__init__

@klayoutmatthias could it be that klayout tries to cast to a Box first here?

klayoutmatthias commented 6 months ago

Grml ... I knew too much smartness would finally create some trouble ... :(

Basically, the whole scheme why this is works is the inference logic KLayout uses: if an argument expects an object, but is passed an array, KLayout tries to call the object constructor from the arguments of the array. This also works for array elements.

This is way (0,0) can create a DPoint in the DPolygon([DPoint, DPoint, ... ]) constructor. In the same fashion a four-element array can be matched to the arguments of DBox in the DPolygon(DBox) constructor. This is what happens here and creates that ambiguity.

I can fix this by giving precedence of array matching array (I think).

The best workaround is to use "assign_hull" which does not have a Box overload:

poly = kf.kdb.DPolygon()
poly.assign_hull([(0,0),[-10,0],[10.,10],[10,0]])

Matthias

joamatab commented 6 months ago

awesome! that worked, thank you Matthias!

feel free to close this

klayoutmatthias commented 6 months ago

Very good, thanks for the feedback.

I'll keep that ticket open to remind me of this stupid ambiguity. I shall be less permissive on the inference paths.

Matthias