param icon indicating copy to clipboard operation
param copied to clipboard

Incorrect type annotation for Integer default

Open MarcSkovMadsen opened this issue 9 months ago • 1 comments

param=2.2.0

Image

My editor is complaining that the default value of the Panel Material UI Checkbox.param.value.default is set to None. The problem is that the type annotation of default is default: float=0!

MarcSkovMadsen avatar Jun 09 '25 06:06 MarcSkovMadsen

This is (unfortunately) not unexpected as param is not typed.

FWIW, this does not error on the types_param branch. But it just postpones the problem down the code to when you want to reassign:

Image

import param
from typing import reveal_type


class P(param.Parameterized):
    a = param.Integer(default=None)
    b = param.Integer()


p = P()

reveal_type(p.a)
p.a = 1
reveal_type(p.a)


reveal_type(p.b)
p.b = 1
reveal_type(p.b)

Haven't figured out if it is possible to solve, with the naive approach done in types_param.

EDIT: I can see I can do it by overloading __set__.

Image

diff --git a/param/parameters.py b/param/parameters.py
index 508159a..f7c08c7 100644
--- a/param/parameters.py
+++ b/param/parameters.py
@@ -877,6 +877,9 @@ class Integer(Number[T]):
                 f"None or an integer value, not {type(step)}."
             )
 
+    @typing.overload
+    def __set__(self, obj, val: int) -> None:
+        ...
 
 class Magnitude(Number[T]):
     """Numeric Parameter required to be in the range [0.0-1.0]."""


hoxbro avatar Jun 09 '25 07:06 hoxbro