Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Add dummy __init__ to ParameterizedFunction #1021

Closed
wants to merge 2 commits into from
Closed

Conversation

hoxbro
Copy link
Member

@hoxbro hoxbro commented Feb 13, 2025

Copy link

codecov bot commented Feb 13, 2025

Codecov Report

Attention: Patch coverage is 50.00000% with 1 line in your changes missing coverage. Please review.

Project coverage is 87.24%. Comparing base (031a4f4) to head (7d95935).
Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
param/parameterized.py 50.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1021      +/-   ##
==========================================
- Coverage   87.25%   87.24%   -0.02%     
==========================================
  Files           9        9              
  Lines        4928     4930       +2     
==========================================
+ Hits         4300     4301       +1     
- Misses        628      629       +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@hoxbro hoxbro requested a review from philippjfr February 13, 2025 12:12
@maximlt
Copy link
Member

maximlt commented Feb 13, 2025

I don't understand :) Is there some reproducible code I could run to validate the issue and that it's fixed with this change? I haven't seen the error reported on Panel.

@hoxbro
Copy link
Member Author

hoxbro commented Feb 13, 2025

I created this environment: mamba env create --name "tmp_param" python="3.12" "holoviews pyright" --offline --yes

pyright example.py
/home/shh/Downloads/example.py
  /home/shh/Downloads/example.py:3:14 - error: Expected 0 positional arguments (reportCallIssue)
1 error, 0 warnings, 0 informations

~/Downloads via 🅒 tmp_paramcat example.py
import holoviews as hv

hv.extension("bokeh")

@maximlt
Copy link
Member

maximlt commented Feb 13, 2025

Ok so here's a reproducer, type checking it with pyright shows that error, mypy doesn't complain.

class Base:
    def __init__(self, **params):
        pass

class FunctionBase(Base):

    # def __init__(self, *args, **kwargs):
    #     super().__init__(*args, **kwargs)

    def __new__(cls, *args, **kwargs) -> Any:
        inst = Base.__new__(cls)
        result = inst.__call__(*args, **kwargs)
        return result

    def __call__(self, *args, **kwargs):
        raise NotImplementedError

class SomeFunction(FunctionBase):
    def __call__(self, *args, **kwargs):
        return 1

SomeFunction('test')

Indeed, adding the dummy __init__ makes pyright happier (and doesn't affect mypy). Not super satisfied with a run time solution to a type checking problem, I asked on pyright's forum if there's another solution. Wait a little bit before merging please.

EDIT:

Ah yeah to explain a little, in Python __init__ is only called if __new__ returns an instance of the class, which is not the case with ParameterizedFunction. That's the sort of thing that doesn't make the life of type checkers easy (ours in turn!).

@hoxbro
Copy link
Member Author

hoxbro commented Feb 13, 2025

Is there any overhead? As far as I see init is never called because of new bypass it.

@maximlt
Copy link
Member

maximlt commented Feb 13, 2025

Yes that's true, still weird no? Pyright's developer answered saying that annotating __new__ with a return type of Any (which I tried but saw no change) should work and was buggy at the moment in Pyright but will be fixed in the next release microsoft/pyright#9907 (comment).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants