Generic strings/regex patterns in strings
Nessuno ha ancora preso questa issue.
- Lingua principale
- Python
- Stelle
- 1.8k
- Fork
- 302
- Merge medio
- 23h
- PR unite (30g)
- 8
Descrizione
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.
Guida per i contributori
Nessuna guida per i contributori indicizzata per questo repository
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Non vengono indicati file di implementazione né test. Inizia esaminando la gestione esistente di str e LiteralStr, insieme alle convenzioni di typing_extensions; chiarisci se la funzionalità utilizza un str generico o un nuovo tipo StrPattern e definisci il comportamento previsto per letterali, variabili, Pattern e Match prima di identificare i test che dimostrano il completamento.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- python
- Ambito
- developer-experience
- Tipo di issue
- Funzionalità
- Difficoltà
- 5/5
- Tempo stimato
- Più di una settimana
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 25/100