Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Runtime Function Signature Validation #11

Closed
keithasaurus opened this issue Dec 31, 2022 · 2 comments · Fixed by #16
Closed

Runtime Function Signature Validation #11

keithasaurus opened this issue Dec 31, 2022 · 2 comments · Fixed by #16
Labels
enhancement New feature or request needs research
Milestone

Comments

@keithasaurus
Copy link
Owner

keithasaurus commented Dec 31, 2022

Motivation

A natural extension of Predicates is to retain some vestige of them on validated types. Then we can do things like

def reverse_long_string(x: Annotated[str, MinLength(20)]) -> Annotated[str, MinLength(20)]:
    return x[::-1]

validator = StringValidator(MinLength(4))

match validator("abc123"):
     case Valid(s):
            reverse_long_string(s)  # type error! Not long enough
     case Invalid():
            ....

Implementation

Presumably this is accomplished through typing.Annotated types. It would change the way we use Annotated. Breaking?

I think a plugin would be needed for Mypy to accomplish this.

Questions

  • Can we develop the proper arithmetic in the type system to not have this feel cumbersome. For instance, can we make it so that
    Annotated[str, MinLength(30)] is allowed when Annotated[str, MinLength(20)] is specified on a parameter?
  • How large is the set of Predicates we'd be able to easily support?
  • Could there be a way for users to specify Predicate arithmetic on their own Predicates?
@keithasaurus keithasaurus added enhancement New feature or request needs research labels Dec 31, 2022
@keithasaurus keithasaurus added this to the 4.0 milestone Dec 31, 2022
@keithasaurus
Copy link
Owner Author

Note that annotated-types is similar. Probably not exactly the same. Should look into

@keithasaurus
Copy link
Owner Author

keithasaurus commented Jan 9, 2023

Sounds like Annotated is not really supported by plugins yet. python/mypy#12619.

For now, the path that might make the most sense is to allow:

@validate_signature
def some_func(a: int, b: Annotated[str, StringValidator(MinLength(20))]) -> str:
     ...

Where int becomes a regular IntValidator and the Annotated StringValidator is used for the second arg.

This requires a few changes, since presumably we want function signature checks to be exact (not coerced):

  • use different type hint resolver to get exact type matches only
  • make changes to allow coercion to be parameterized

@keithasaurus keithasaurus modified the milestones: 4.0, 3.x Jan 12, 2023
@keithasaurus keithasaurus changed the title Refinement Types Runtime Type-Checking Jan 17, 2023
@keithasaurus keithasaurus changed the title Runtime Type-Checking Runtime Function Signature Validation Jan 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request needs research
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant