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

Added missing super().__init_subclass__ call in _ParameterBase.__init_subclass__ #969

Merged
merged 1 commit into from
Oct 8, 2024

Conversation

JRRudy1
Copy link
Contributor

@JRRudy1 JRRudy1 commented Oct 7, 2024

Currently the _ParameterBase.__init_subclass__ method does not forward to the method on its superclass, so any other base classes in the MRO do not have a chance to perform any necessary subclass initialization steps. This PR adds the missing call.

This is particular relevant when defining a Parameter subclass with generic type parameters by inheriting from typing.Generic (or equivalently by using the new generics syntax added in Python 3.12). Inheriting from typing.Generic[T] where T is a typing.TypeVar typically allows a class to be parameterized by a concrete type, but this does not work when _ParameterBase is in the MRO since the __init_subclass__ call is not properly propagated up the MRO.

The following example raises an AttributeError in the current param release, but works correctly after this PR:

from typing import Generic, TypeVar
from param import Selector

T = TypeVar('T')

class GenericSelector(Selector, Generic[T]):
    pass

# raises `AttributeError: type object 'GenericSelector' has no attribute '__parameters__'`:
IntSelector = GenericSelector[int]

Copy link

codecov bot commented Oct 7, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 86.45%. Comparing base (e54dd51) to head (0a249b2).
Report is 1 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #969   +/-   ##
=======================================
  Coverage   86.45%   86.45%           
=======================================
  Files          10       10           
  Lines        5176     5177    +1     
=======================================
+ Hits         4475     4476    +1     
  Misses        701      701           

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

@MarcSkovMadsen
Copy link
Collaborator

If using generics does mypy and other type checkers then know an IntSelector parameter is an int?

@JRRudy1
Copy link
Contributor Author

JRRudy1 commented Oct 8, 2024

If using generics does mypy and other type checkers then know an IntSelector parameter is an int?

Probably not... unfortunately the param library doesn't seem to integrate very well with static type checkers, but hopefully it will get better someday (I was about to link issue #​376 before realizing that was you haha). I can often trick my IDE (PyCharm) by declaring params like a: int = IntSelector(...), or a: T = GenericSelector(...) in a generic context, though it is far from perfect and probably doesn't fly with mypy. But even in cases where generics (and type-hints in general) don't serve a practical purpose, they can often significantly help with readability/maintainability anyways.

That said, the motivation for this PR isn't really specific to handling generic classes, that just happened to be the case that led me here.

Copy link
Member

@maximlt maximlt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like an oversight, thanks!

@maximlt maximlt merged commit f37da47 into holoviz:main Oct 8, 2024
10 of 11 checks passed
@maximlt maximlt added this to the v2.1.2 milestone Oct 8, 2024
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.

3 participants