Generic strings/regex patterns in strings
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 1.8k
- Forks
- 302
- Avg merge
- 23h
- Merged PRs (30d)
- 8
Description
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.
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
No implementation files or tests are named. Start by reviewing the existing handling of str and LiteralStr, along with typing_extensions conventions; clarify whether the feature uses a generic str or a new StrPattern type, and define expected behavior for literals, variables, Pattern, and Match before identifying tests that demonstrate completion.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- developer-experience
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 25/100