Replies: 4 comments 14 replies
-
I forgot to add ellipsis support to getitem/setitem, so a valid expression like this: h[...] = np.ones(10) will fail a type check. There isn't a PEP 484 way to type check an ellipsis; only |
Beta Was this translation helpful? Give feedback.
-
When subclassing import boost_histogram as bh
import numpy as np
class NewHistogram(bh.Histogram, family=object()):
def __init__(self, hist: bh.Histogram) -> None:
super().__init__(hist, metadata=None)
@property
def x(self) -> np.ndarray:
return self.values()
def f(hist: NewHistogram) -> None:
...
h = bh.Histogram(bh.axis.Variable([0, 10]), storage=bh.storage.Weight())
h.fill([1,2,3])
a = NewHistogram(h)
f(a)
a.x The constructor is now needed to avoid the error: No overload variant of "NewHistogram" matches argument type "Histogram" [call-overload] Previously ( I am unsure in particular about Sorry I previously missed this discussion topic for type-related issues when opening #527! |
Beta Was this translation helpful? Give feedback.
-
Generic histograms actually would work pretty well - MyPy can infer the type of the histogram from the arguments, so from __future__ import annotations
from typing import Generic, NewType, TypeVar
class A: pass
class B: pass
class C: pass
AView = NewType("AView", int)
BView = NewType("BView", int)
S = TypeVar("S", A, B, C)
class MyGeneric(Generic[S]):
def __init__(self, s: S):
self.s: S = s
def storage(self) -> S:
return self.s
def value(self) -> AView | BView:
if isinstance(self.s, A):
return AView(0)
else:
return BView(0)
gen = MyGeneric(A())
value: AView = gen.value()
# Produces:
# : Incompatible types in assignment (expression has type "Union[AView, BView]", variable has type "AView") I don't know what to write instead of the Union there. |
Beta Was this translation helpful? Give feedback.
-
Running A workaround is not checking boost-histogram via mypy --follow-imports skip . This means the following would only succeed with a # type: ignore , since otherwise the family kwarg is unknown:
import boost_histogram as bh
class Hist(bh.Histogram, family=object()):
... Would the recommendation be to not run |
Beta Was this translation helpful? Give feedback.
-
This is a holder for any problems or questions that come up about boost-histogram's static typing support.
Beta Was this translation helpful? Give feedback.
All reactions