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

Generic strings/regex patterns in strings #1202

Open
Fidget-Spinner opened this issue May 31, 2022 · 2 comments
Open

Generic strings/regex patterns in strings #1202

Fidget-Spinner opened this issue May 31, 2022 · 2 comments
Labels
topic: feature Discussions about new features for Python's type annotations

Comments

@Fidget-Spinner
Copy link
Member

Fidget-Spinner commented May 31, 2022

Currently, we have str and LiteralStr. These don't do a good job expressing the range of values a string may have. Let's say we have the following function:

Email: TypeAlias = str

def send_email(email: Email) -> int:
    # Send an email
    ...

# passes
send_email("test@example.com")

# passes
send_email("badddd")

# passes even if unknown_var is only identified as `str`
send_email(unknown_var)

We could improve it by having something like this:

# This is a bad regex pattern, but you get the idea :)
Email: TypeAlias = str['[a-z0-9]+@[a-z0-9]+\.[a-z0-9]+']

def send_email(email: Email) -> int:
    # Send an email
    ...

# passes
send_email("test@example.com")

# fails
send_email("badddd")

# passes even if unknown_var is only identified as `str`
send_email(unknown_var)

A static type checker would be able to validate strings passed in by code. I would imagine this idea can be extended to Pattern and Match generics as well, but I haven't thought too deeply about them yet.

If we don't want to make str generic, we could add a new type to typing called StrPattern. We would need it anyways to backport to typing_extensions.

@Fidget-Spinner Fidget-Spinner added the topic: feature Discussions about new features for Python's type annotations label May 31, 2022
@henribru
Copy link

henribru commented May 31, 2022

There's kind of some prior art in Typescript: https://www.typescriptlang.org/docs/handbook/2/template-literal-types.html

It's not quite as powerful as this regex idea though.

@gvanrossum
Copy link
Member

gvanrossum commented May 31, 2022

The TypeScript feature allows more than type checking though -- IIRC it allows constructing new (string literal) types from other strings.

I personally don't think a feature to let type checkers use regex matching on literals is all that useful -- I'd rather use Email = NewType(str) and leave the validation to runtime code that the type checker doesn't have to be understand. (It's likely that you already have an email validation routine in your system, and it may not be easy to replicate its exact functionality as a regex.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic: feature Discussions about new features for Python's type annotations
Projects
None yet
Development

No branches or pull requests

3 participants