plum icon indicating copy to clipboard operation
plum copied to clipboard

Parametric return types not working

Open nstarman opened this issue 5 months ago • 3 comments

Hi! I'm getting some warnings about return types for subclasses of Generic and is says to open an issue...

The error is approximately


class Object(Generic[T]):
    def method(self) -> T: ...

class A(): ...

@dispatch
def function(...) -> Object[A]: ...

The error is

UserWarning: Could not resolve the type hint of `coordinax._src.vectors.collection.core.KinematicSpace[coordinax._src.vectors.d3.cylindrical.CylindricalPos]`. I have ended the resolution here to not make your code break, but some types might not be working correctly. Please open an issue at https://github.com/beartype/plum.

The full problem can be found in the CI for https://github.com/GalacticDynamics/coordinax/pull/443.

nstarman avatar Jul 01 '25 17:07 nstarman

GH isn't letting me attach this doc so here's a snippet

Details

2025-07-01T17:40:32.2249438Z tests/test_package.py [32m.[0m[31m [100%][0m 2025-07-01T17:40:32.2250023Z 2025-07-01T17:40:32.2250231Z =================================== FAILURES =================================== 2025-07-01T17:40:32.2251037Z [31m[1m____________________ frame_transforms.py line=186 column=1 _____________________[0m 2025-07-01T17:40:32.2251528Z 2025-07-01T17:40:32.2252925Z Example at /home/runner/work/coordinax/coordinax/src/coordinax/_coordinax_space_frames/frame_transforms.py, line 186, column 1 did not evaluate as expected: 2025-07-01T17:40:32.2254043Z Exception raised: 2025-07-01T17:40:32.2254392Z Traceback (most recent call last): 2025-07-01T17:40:32.2262668Z File "/home/runner/.local/share/uv/python/cpython-3.11.13-linux-x86_64-gnu/lib/python3.11/doctest.py", line 1355, in __run 2025-07-01T17:40:32.2263839Z exec(compile(example.source, filename, "single", 2025-07-01T17:40:32.2265447Z File "<doctest /home/runner/work/coordinax/coordinax/src/coordinax/coordinax_space_frames/frame_transforms.py[0]>", line 1, in 2025-07-01T17:40:32.2266826Z vega_q = cx.vecs.LonLatSphericalPos.from(vega.icrs.data) 2025-07-01T17:40:32.2267381Z ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 2025-07-01T17:40:32.2268348Z File "/home/runner/work/coordinax/coordinax/.venv/lib/python3.11/site-packages/plum/function.py", line 509, in call 2025-07-01T17:40:32.2269308Z return self._f(self._instance, *args, **kw_args) 2025-07-01T17:40:32.2269785Z ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 2025-07-01T17:40:32.2270722Z File "/home/runner/work/coordinax/coordinax/.venv/lib/python3.11/site-packages/plum/function.py", line 383, in call 2025-07-01T17:40:32.2271686Z return _convert(method(*args, **kw_args), return_type) 2025-07-01T17:40:32.2272190Z ^^^^^^^^^^^^^^^^^^^^^^^^ 2025-07-01T17:40:32.2273446Z File "/home/runner/work/coordinax/coordinax/.venv/lib/python3.11/site-packages/jaxtyping/_decorator.py", line 549, in wrapped_fn 2025-07-01T17:40:32.2274509Z return wrapped_fn_impl(args, kwargs, bound, memos) 2025-07-01T17:40:32.2275025Z ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 2025-07-01T17:40:32.2276084Z File "/home/runner/work/coordinax/coordinax/.venv/lib/python3.11/site-packages/jaxtyping/_decorator.py", line 473, in wrapped_fn_impl 2025-07-01T17:40:32.2277111Z out = fn(*args, **kwargs) 2025-07-01T17:40:32.2277482Z ^^^^^^^^^^^^^^^^^^^ 2025-07-01T17:40:32.2278320Z File "/home/runner/work/coordinax/coordinax/src/coordinax/src/vectors/base/vector.py", line 612, in from 2025-07-01T17:40:32.2279184Z return vector(cls, *args, **kwargs) 2025-07-01T17:40:32.2279604Z ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 2025-07-01T17:40:32.2280539Z File "/home/runner/work/coordinax/coordinax/.venv/lib/python3.11/site-packages/plum/function.py", line 383, in call 2025-07-01T17:40:32.2281604Z return _convert(method(*args, **kw_args), return_type) 2025-07-01T17:40:32.2282174Z ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 2025-07-01T17:40:32.2283337Z File "/home/runner/work/coordinax/coordinax/.venv/lib/python3.11/site-packages/plum/function.py", line 42, in _convert 2025-07-01T17:40:32.2284279Z return _promised_convert(obj, target_type) 2025-07-01T17:40:32.2284735Z ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 2025-07-01T17:40:32.2285661Z File "/home/runner/work/coordinax/coordinax/.venv/lib/python3.11/site-packages/plum/function.py", line 383, in call 2025-07-01T17:40:32.2286641Z return _convert(method(*args, **kw_args), return_type) 2025-07-01T17:40:32.2287130Z ^^^^^^^^^^^^^^^^^^^^^^^^ 2025-07-01T17:40:32.2288061Z File "/home/runner/work/coordinax/coordinax/.venv/lib/python3.11/site-packages/plum/promotion.py", line 49, in convert 2025-07-01T17:40:32.2289066Z return _convert.invoke(type(obj), type_to)(obj, type_to) 2025-07-01T17:40:32.2289589Z ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 2025-07-01T17:40:32.2290585Z File "/home/runner/work/coordinax/coordinax/.venv/lib/python3.11/site-packages/plum/function.py", line 433, in wrapped_method 2025-07-01T17:40:32.2291629Z return _convert(method(*args, **kw_args), return_type) 2025-07-01T17:40:32.2292132Z ^^^^^^^^^^^^^^^^^^^^^^^^ 2025-07-01T17:40:32.2293361Z File "/home/runner/work/coordinax/coordinax/.venv/lib/python3.11/site-packages/plum/promotion.py", line 83, in perform_conversion 2025-07-01T17:40:32.2294342Z return f(obj) 2025-07-01T17:40:32.2294652Z ^^^^^^ 2025-07-01T17:40:32.2295566Z File "/home/runner/work/coordinax/coordinax/.venv/lib/python3.11/site-packages/jaxtyping/_decorator.py", line 549, in wrapped_fn 2025-07-01T17:40:32.2296619Z return wrapped_fn_impl(args, kwargs, bound, memos) 2025-07-01T17:40:32.2297359Z ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 2025-07-01T17:40:32.2298615Z File "/home/runner/work/coordinax/coordinax/.venv/lib/python3.11/site-packages/jaxtyping/_decorator.py", line 473, in wrapped_fn_impl 2025-07-01T17:40:32.2299669Z out = fn(*args, **kwargs) 2025-07-01T17:40:32.2300069Z ^^^^^^^^^^^^^^^^^^^ 2025-07-01T17:40:32.2301285Z File "/home/runner/work/coordinax/coordinax/src/coordinax/_src/vectors/d3/register_convert.py", line 147, in convert_math_spherical_to_lonlat_spherical 2025-07-01T17:40:32.2302707Z return vconvert(LonLatSphericalPos, pos) 2025-07-01T17:40:32.2303228Z ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 2025-07-01T17:40:32.2304186Z File "/home/runner/work/coordinax/coordinax/.venv/lib/python3.11/site-packages/plum/function.py", line 382, in call 2025-07-01T17:40:32.2305214Z method, return_type = self._resolve_method_with_cache(args=args) 2025-07-01T17:40:32.2305795Z ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 2025-07-01T17:40:32.2306881Z File "/home/runner/work/coordinax/coordinax/.venv/lib/python3.11/site-packages/plum/function.py", line 399, in _resolve_method_with_cache 2025-07-01T17:40:32.2307925Z self._resolve_pending_registrations() 2025-07-01T17:40:32.2309077Z File "/home/runner/work/coordinax/coordinax/.venv/lib/python3.11/site-packages/plum/function.py", line 288, in _resolve_pending_registrations 2025-07-01T17:40:32.2310235Z submethod = Method(f, subsignature, function_name=self.name) 2025-07-01T17:40:32.2310774Z ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 2025-07-01T17:40:32.2311720Z File "/home/runner/work/coordinax/coordinax/.venv/lib/python3.11/site-packages/plum/method.py", line 49, in init 2025-07-01T17:40:32.2312894Z return_type = extract_return_type(implementation) 2025-07-01T17:40:32.2313399Z ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 2025-07-01T17:40:32.2314439Z File "/home/runner/work/coordinax/coordinax/.venv/lib/python3.11/site-packages/plum/method.py", line 222, in extract_return_type 2025-07-01T17:40:32.2315532Z return_type = resolve_type_hint(sig.return_annotation) 2025-07-01T17:40:32.2316063Z ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 2025-07-01T17:40:32.2317062Z File "/home/runner/work/coordinax/coordinax/.venv/lib/python3.11/site-packages/plum/type.py", line 238, in resolve_type_hint 2025-07-01T17:40:32.2318000Z warnings.warn( 2025-07-01T17:40:32.2319966Z UserWarning: Could not resolve the type hint of coordinax._src.vectors.collection.core.KinematicSpace[coordinax._src.vectors.d3.cylindrical.CylindricalPos]. I have ended the resolution here to not make your code break, but some types might not be working correctly. Please open an issue at https://github.com/beartype/plum.

nstarman avatar Jul 01 '25 21:07 nstarman

Hey @nstarman! Generics unfortunately aren't yet supported in this way, which is why you're seeing the warning. :( Perhaps we should specialise the warning so that it says this.

I might soon have capacity to more actively start working on this again. Perhaps seeing if generics in this way could be supported would be a good thing to do first.

wesselb avatar Jul 04 '25 12:07 wesselb

Thanks @wesselb, that would be great. I'd love to be using plum with Generic types. Especially in return annotations.

nstarman avatar Jul 21 '25 19:07 nstarman