Generic strings/regex patterns in strings
Nadie ha tomado este issue todavía.
- Lenguaje dominante
- Python
- Estrellas
- 1.8k
- Forks
- 302
- Merge medio
- 23 h
- PR fusionados (30 d)
- 8
Descripción
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.
Guía de contribución
No hay ninguna guía de contribución indexada para este repositorio
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Línea de trabajo
No se nombran archivos de implementación ni pruebas. Empieza revisando el manejo existente de str y LiteralStr, junto con las convenciones de typing_extensions; aclara si la funcionalidad utiliza un str genérico o un nuevo tipo StrPattern, y define el comportamiento esperado para literales, variables, Pattern y Match antes de identificar las pruebas que demuestren que se ha completado.
Escrito por el modelo de indexación a partir del texto del issue.
Evaluación
- Stack tecnológico
- python
- Área
- developer-experience
- Tipo de issue
- Nueva funcionalidad
- Dificultad
- 5/5
- Tiempo estimado
- Más de una semana
- Estado de actividad
- Estancado
- Claridad
- Bastante claro
- Aptitud para principiantes
- 25/100