Generic strings/regex patterns in strings

Aperta
#1,202 8 commenti 13 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
5/5
Tempo stimato
Più di una settimana
Idoneità per principianti
25/100
Tipo di issue
Funzionalità
Chiarezza
Abbastanza chiara
Stato di attività
Ferma
Stack tecnologico
python

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.

Descrizione

topic: feature

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.

Lingua principale
Python
Stelle
1.8k
Fork
302
Merge medio
23h
PR unite (30g)
8

Guida per i contributori

Nessuna guida per i contributori indicizzata per questo repository

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di python/typing

Tutte le issue di python/typing

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.