ITADN

Return types of `Color`'s methods.

#467Closedbeskep 创建于 2025-04-03
S: triage
B
beskepcommented
The official doc recommends inheriting `Color` for color space registration. This can cause problems with some methods. ```python from coloraide import Color from coloraide.spaces.okhsl import Okhsl class InheritedColor(Color): ... InheritedColor.register(Okhsl()) if __name__ == '__main__': color = InheritedColor('srgb', [42, 42, 42], 0) color = color.convert('okhsl') # * Mypy assignment error # Incompatible types in assignment (expression has type "Color", variable has type "InheritedColor") # but color is InheritedColor assert type(color) is not Color assert isinstance(color, InheritedColor) ``` The return types of `.new()`, `.clone()`, `.convert()`, ... should be `Self`, not `Color`.
关闭于 2025-04-04 4 条评论