Skip to content

Support for arbitraty generics#416

Merged
pschanely merged 8 commits into
pschanely:mainfrom
rambip:main
May 27, 2026
Merged

Support for arbitraty generics#416
pschanely merged 8 commits into
pschanely:mainfrom
rambip:main

Conversation

@rambip
Copy link
Copy Markdown
Contributor

@rambip rambip commented May 24, 2026

This is my attempt to fix #415

To be clear:

  • I was in a rush, the solution was LLM-generated
  • I did not review the code
  • I only tested on my toy-example

If you consider it's slop and close it, I will have no problem with that.

Have a nice day !

Unwrap _GenericAlias to its origin class before calling get_type_hints,
get_constructor_signature, and WithEnforcement, all of which require a
plain class rather than a parameterized alias.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@rambip
Copy link
Copy Markdown
Contributor Author

rambip commented May 24, 2026

Hum, it does not seem to solve this case right now:
now it does

def accuracy(model: nn.Module, loader: DataLoader[tuple[Tensor, Tensor]]) -> float:
    """Compute accuracy on a dataset.

    pre: loader is not None and len(loader) > 0
    inv: 0 <= return_value <= 1
    """
    correct = 0
    total = 0
    for X, y in loader:
        preds = model(X).argmax(dim=1)
        correct += (preds == y).sum().item()
        total += len(y)
    return correct / total

@rambip rambip marked this pull request as draft May 24, 2026 15:25
rambip and others added 2 commits May 24, 2026 17:31
…tor sig

When proxy_for_class receives Container[int], use get_bindings_from_type_arguments
to build {T: int} and realize() each constructor parameter annotation before
gen_args sees it, so the generated proxy has the correct concrete types.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Classes like torch DataLoader have forward references that can't be resolved
outside their defining module, causing NameError. Fall back to empty dict so
proxy_for_class can still attempt construction via the constructor signature.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@rambip rambip marked this pull request as ready for review May 24, 2026 15:44
Copy link
Copy Markdown
Owner

@pschanely pschanely left a comment

Choose a reason for hiding this comment

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

Not slop, as far as I can tell. Thank you for the contribution!
I have a few (hopefully small) requests; feel free to add yourself at the bottom of contributing.rst too.

Comment thread crosshair/core.py Outdated
Comment thread crosshair/core.py
Comment thread crosshair/core.py Outdated
Comment thread crosshair/core.py
rambip and others added 3 commits May 27, 2026 08:07
- Narrow get_type_hints exception handling to (AttributeError, NameError)
- Move TypeVar KeyError fallback into dynamic_typing.realize via .get()
- Add tests for parameterized generic proxies, multi-TypeVar, and
  unresolvable forward ref fallback
- Add contributor entry for Antonin Peronnet

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Consistent with existing test file style.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@rambip
Copy link
Copy Markdown
Contributor Author

rambip commented May 27, 2026

Is that ok now ?

@pschanely
Copy link
Copy Markdown
Owner

Is that ok now ?

Just a few more mypy typing issues, but otherwise this looks great!
FWIW, in many cases, I just remove the -> None from the test definition to disable checking for tests that are painful to get compliant.

@rambip
Copy link
Copy Markdown
Contributor Author

rambip commented May 27, 2026

I'm confused about the mypy errors, some seems unrelated to my change.

The last commit added # type: ignore[union-attr] but the actual
mypy error is attr-defined, not union-attr.

Co-authored-by: DeepSeek <noreply@ai.dev>
@pschanely
Copy link
Copy Markdown
Owner

I'm confused about the mypy errors, some seems unrelated to my change.

Unfortunately there are a lot of warning messages in there (I'll file an issue about this presently); for now, you only need to look at the errors.

@rambip
Copy link
Copy Markdown
Contributor Author

rambip commented May 27, 2026

Only hypothesis failing

Copy link
Copy Markdown
Owner

@pschanely pschanely left a comment

Choose a reason for hiding this comment

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

LGTM! Thank you!

@pschanely pschanely merged commit f2a8d49 into pschanely:main May 27, 2026
13 of 14 checks passed
pschanely pushed a commit that referenced this pull request May 27, 2026
- Narrow get_type_hints exception handling to (AttributeError, NameError)
- Move TypeVar KeyError fallback into dynamic_typing.realize via .get()
- Add tests for parameterized generic proxies, multi-TypeVar, and
  unresolvable forward ref fallback
- Add contributor entry for Antonin Peronnet

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
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.

Support generics in function signature

2 participants