question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

nested NamedTuples don't work with beartype + jaxtyping

See original GitHub issue

This works

from beartype import beartype as typechecker
from jaxtyping import Array, Float, jaxtyped
from typing import NamedTuple, Union, Optional

@typechecker
class ParamsLGSSMInitial(NamedTuple):
    mean: float 
    cov: float 

p = ParamsLGSSMInitial(mean=5.0, cov=10.0) # fine

@typechecker
class ParamsLGSSM(NamedTuple): # fine
    initial: ParamsLGSSMInitial

but this does not

@jaxtyped
@typechecker
class ParamsLGSSMInitial2(NamedTuple):
    mean: float 
    cov: float 

p = ParamsLGSSMInitial2(mean=5.0, cov=10.0) # fine

@typechecker
class ParamsLGSSM(NamedTuple): 
    initial2: ParamsLGSSMInitial2 # fail

I get the error

beartype.roar.BeartypeDecorHintNonpepException: @beartyped namedtuple_ParamsLGSSM.ParamsLGSSM.__new__() parameter "initial2" type hint <jaxtyping.decorator._Jaxtyped object at 0x7f7ae90450a0> either PEP-noncompliant or currently unsupported by @beartype.

I realize this may be a problem with jaxtyping and not beartype… (Also I realize I don’t need the @jaxtyped decorator in this example, but in the real code, float is actually an Array.)

@patrick-kidger @slinderman

Issue Analytics

  • State:closed
  • Created 10 months ago
  • Reactions:1
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
patrick-kidgercommented, Nov 13, 2022

So for context,jaxtyped(fn) returns a functools.wraps(fn)(_Jaxtyped(fn)). I feel like that should still be a valid type annotation (whether or not beartype respects the wraps).

FWIW one possible fix might be to make jaxtyped of a class actually just wrap the __init__ method.

I’d be happy to accept a PR on this over in the jaxtyping repo.

0reactions
leyceccommented, Nov 15, 2022

I think this should be fixed…

A design goal for jaxtyping is to avoid the need for that kind of close coupling!

The sweet words I’ve waited a lifetime to hear. 🥰 😍 😻 :heart_on_fire:

The ML Dream Team of @murphyk + @patrick-kidger capably resolved this without my needing to lift a paw. Thanks so much for resolving this for me. I can now quietly close this and pretend I knew what was going on here.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Iterating a list of nested named tuples causes type error #7902
I am reporting a bug. Consider this code: from typing import List, NamedTuple, Union class A(NamedTuple): p: int class B(NamedTuple): q: int ...
Read more >
Name a nested typing.NamedTuple in the same outer class in ...
I have a class A which has nested typing.NamedTuple s B and C. So far so good. The problem arises when the nested...
Read more >
Write Pythonic and Clean Code With namedtuple - Real Python
Have a helpful docstring based on the type and field names; Provide a helpful string representation that prints the tuple content in a...
Read more >
Release develop Evan Hubinger - Coconut
How do I use a runtime type checker like beartype when Coconut seems to compile all ... I don't like functional programming, should...
Read more >
++namedtuple - Python-style Named Tuples in C++20
https://cppcon.org/https://github.com/CppCon/CppCon2021---In this workshop, we will take a look into how Python-style named tuples can be ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found