Broken Recursive Typing for Python

I have a type alias that I call point2d and a related recursive type alias.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>point2d = tuple[float, float]
point2d_nested = (
point2d
| list["point2d_nested"]
| tuple["point2d_nested", ...]
)
</code>
<code>point2d = tuple[float, float] point2d_nested = ( point2d | list["point2d_nested"] | tuple["point2d_nested", ...] ) </code>
point2d = tuple[float, float]

point2d_nested = (
    point2d
    | list["point2d_nested"]
    | tuple["point2d_nested", ...]
)

Now I want to write a function that will scale points in any nested structure by a factor. For example, scale_nested((1,1), 2) == (2,2) or scale_nested([(1,1), (1,1)]) == [(2,2), (2,2)]. Here’s an implementation:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>def scale_nested(points: point2d_nested, factor: float) -> point2d_nested:
"""
Scales a list of lists of points by a given factor.
Weird things happen if you supply 3D points or if your point is not a tuple.
"""
if not points:
return points
# We've bottomed out at a point
if (
isinstance(points, tuple)
and len(points) == 2
and isinstance(points[0], (int, float))
and isinstance(points[1], (int, float))
):
return points[0] * factor, points[1] * factor
if isinstance(points, list):
return [scale_nested(p, factor) for p in points]
if isinstance(points, tuple):
return tuple(
scale_nested(p, factor) for p in points if not isinstance(p, (int, float))
)
raise ValueError(f"Something is an invalid type: {points}")
</code>
<code>def scale_nested(points: point2d_nested, factor: float) -> point2d_nested: """ Scales a list of lists of points by a given factor. Weird things happen if you supply 3D points or if your point is not a tuple. """ if not points: return points # We've bottomed out at a point if ( isinstance(points, tuple) and len(points) == 2 and isinstance(points[0], (int, float)) and isinstance(points[1], (int, float)) ): return points[0] * factor, points[1] * factor if isinstance(points, list): return [scale_nested(p, factor) for p in points] if isinstance(points, tuple): return tuple( scale_nested(p, factor) for p in points if not isinstance(p, (int, float)) ) raise ValueError(f"Something is an invalid type: {points}") </code>
def scale_nested(points: point2d_nested, factor: float) -> point2d_nested:
    """
    Scales a list of lists of points by a given factor.
    Weird things happen if you supply 3D points or if your point is not a tuple.
    """
    if not points:
        return points

    # We've bottomed out at a point
    if (
        isinstance(points, tuple)
        and len(points) == 2
        and isinstance(points[0], (int, float))
        and isinstance(points[1], (int, float))
    ):
        return points[0] * factor, points[1] * factor

    if isinstance(points, list):
        return [scale_nested(p, factor) for p in points]

    if isinstance(points, tuple):
        return tuple(
            scale_nested(p, factor) for p in points if not isinstance(p, (int, float))
        )

    raise ValueError(f"Something is an invalid type: {points}")

Unfortunately, this doesn’t work:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>knots = [((1.0, 2.0),)]
scale_nested(knots, 2) # <- knots look red
</code>
<code>knots = [((1.0, 2.0),)] scale_nested(knots, 2) # <- knots look red </code>
knots = [((1.0, 2.0),)]
scale_nested(knots, 2) # <- knots look red
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>Argument of type "list[tuple[tuple[float, float]]]" cannot be assigned to parameter "points" of type "point2d_nested" in function "scale_nested"
Type "list[tuple[tuple[float, float]]]" cannot be assigned to type "point2d_nested"
"list[tuple[tuple[float, float]]]" is incompatible with "point2d"
"list[tuple[tuple[float, float]]]" is incompatible with "list[point2d_nested]"
Type parameter "_T@list" is invariant, but "tuple[tuple[float, float]]" is not the same as "point2d_nested"
Consider switching from "list" to "Sequence" which is covariant
"list[tuple[tuple[float, float]]]" is incompatible with "tuple[point2d_nested, ...]"PylancereportGeneralTypeIssues
(variable) knots: list[tuple[tuple[float, float]]]
</code>
<code>Argument of type "list[tuple[tuple[float, float]]]" cannot be assigned to parameter "points" of type "point2d_nested" in function "scale_nested" Type "list[tuple[tuple[float, float]]]" cannot be assigned to type "point2d_nested" "list[tuple[tuple[float, float]]]" is incompatible with "point2d" "list[tuple[tuple[float, float]]]" is incompatible with "list[point2d_nested]" Type parameter "_T@list" is invariant, but "tuple[tuple[float, float]]" is not the same as "point2d_nested" Consider switching from "list" to "Sequence" which is covariant "list[tuple[tuple[float, float]]]" is incompatible with "tuple[point2d_nested, ...]"PylancereportGeneralTypeIssues (variable) knots: list[tuple[tuple[float, float]]] </code>
Argument of type "list[tuple[tuple[float, float]]]" cannot be assigned to parameter "points" of type "point2d_nested" in function "scale_nested"
  Type "list[tuple[tuple[float, float]]]" cannot be assigned to type "point2d_nested"
    "list[tuple[tuple[float, float]]]" is incompatible with "point2d"
    "list[tuple[tuple[float, float]]]" is incompatible with "list[point2d_nested]"
      Type parameter "_T@list" is invariant, but "tuple[tuple[float, float]]" is not the same as "point2d_nested"
      Consider switching from "list" to "Sequence" which is covariant
    "list[tuple[tuple[float, float]]]" is incompatible with "tuple[point2d_nested, ...]"PylancereportGeneralTypeIssues
(variable) knots: list[tuple[tuple[float, float]]]

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật