Annotated metadata#389
Conversation
Expand Unpack/GroupedMetadata metadata flattening and strengthen annotated-types tests.
|
Separate to above, noted the following failures during test suite run: Tests run:
Full suite failures (no watcher changes in this PR):
Summary: 3 failed, 11919 passed, 5 skipped, 3 xfailed. |
Support typing_extensions get_origin/get_args for Annotated metadata and skip typing objects as predicates.
Fix Unpack expansion under Python 3.9 by relying on compat get_args in annotated metadata flattening.
Handle Unpack aliases more robustly across typing/typing_extensions and rely on Black formatting from pre-commit.
pschanely
left a comment
There was a problem hiding this comment.
Thank you SO MUCH for doing this! (and for your patience with my slow response) I've got just a few minor comments below.
Oh also, feel free to add yourself to the contributors list in doc/source/contributing.rst and link it to whatever you like!
| # - Avoid typing.get_type_hints(): CrossHair may run under tracing where | ||
| # interacting with typing internals can raise. |
There was a problem hiding this comment.
I wouldn't have thought that tracing should be on when we're extracting conditions. (and if it breaks under tracing, that's also a bug I'd love to file)
Do you remember under what circumstances you saw an error?
| try: | ||
| import typing_extensions as _typing_extensions # type: ignore | ||
| except ModuleNotFoundError: | ||
| _typing_extensions = None # type: ignore |
There was a problem hiding this comment.
typing_extensions is an official dependency, so we don't need to guard the import (or handle None cases below)
| PRECONDITION, | ||
| _eval_param, | ||
| filename, | ||
| first_line, |
There was a problem hiding this comment.
In an ideal world, we'd find the line number of the annotation, in case the signature extends over multiple lines. Unfortunately, I think that's going to be pretty annoying, so maybe we call this close enough!
Summary
including grouped metadata (Interval / Len).
Notes
Tests
Issue #201 comment
I’ve prepared a PR that adds typing.Annotated metadata support to CrossHair.
It treats Annotated metadata as contracts (params -> preconditions, returns ->
postconditions, class attributes -> invariants), supports annotated_types
metadata (Gt / Ge / Lt / Le / MultipleOf / MinLen / MaxLen / Predicate + Interval / Len via
GroupedMetadata), and accepts custom metadata via is_valid() or 1‑arg callables.
Tests: