danielgtaylor / danielgtaylor/python-betterproto
Mypy type checking does not work with enums
- Lenguaje dominante
- Python
- Estrellas
- 1.8k
- Forks
- 234
- Métricas de merge de PR
- Sin PR fusionados en 30 d
Descripción
### Summary
When using an enum in a code project mypy is broken and has the impression that all the enum fields are of type 'int'
### Reproduction Steps
Run type checking on this file and see the problem in version 2.0.0b7
```py
import betterproto
class Colour(betterproto.Enum):
RED = 1
GREEN = 2
BLUE = 3
def print_color(color: Colour) -> None:
print(color.value)
def test_mypy_types() -> None:
print(Colour.RED == Colour.try_value(Colour.RED))
print_color(Colour.RED)
print_color(Colour.try_value(Colour.RED))
if __name__ == '__main__':
test_mypy_types()
```
### Expected Results
I would have expected the type checking to pass as in version 2.0.0-beta6
This is caused by #293 (and is not solved by #540)
The expected type of `Colour.RED` is a `Colour` not an `int`.
### Actual Results
```bash
$ poetry run mypy test-mypy.py
test-mypy.py:16: error: Argument 1 to "print_color" has incompatible type "int"; expected "Colour" [arg-type]
print_color(Colour.RED)
^~~~~~~~~~
Found 1 error in 1 file (checked 1 source file)
```
### System Information
```bash
Python 3.11.9
Name: betterproto
Version: 2.0.0b7
Summary: A better Protobuf / gRPC generator & library
Home-page: https://github.com/danielgtaylor/python-betterproto
Author: Daniel G. Taylor
Author-email: danielgtaylor@gmail.com
License: MIT
Location: /home/jns/product/.venv/lib/python3.11/site-packages
Requires: grpclib, python-dateutil, typing-extensions
Required-by: inventory_code_project_enumeration, inventory_name_inference, inventory_py_schema_description, inventory_py_schema_endpoints_extraction, inventory_py_schema_services_extraction, inventory_repository_analysis, inventory_repository_clone, inventory_repository_extract_schema, inventory_repository_extract_secret, inventory_repository_extract_service, inventory_repository_owner_search, inventory_schema_analysis, inventory_schema_classification, inventory_schema_conversion, inventory_schema_data_leaks, inventory_service_description, inventory_software_types_fingerprinter, protocol
```
### Checklist
- [X] I have searched the issues for duplicates.
- [X] I have shown the entire traceback, if possible.
- [X] I have verified this issue occurs on the latest prelease of betterproto which can be installed using `pip install -U --pre betterproto`, if possible.
Guía de contribución
Línea de trabajo
Empieza ejecutando mypy en la reproducción test-mypy.py proporcionada con la versión afectada de betterproto y, a continuación, inspecciona los cambios de tipado de enum asociados con #293 y #540. Se considera terminado cuando mypy trata Colour.RED y Colour.try_value(Colour.RED) como valores Colour y el ejemplo pasa sin el error de argument-type indicado.
Escrito por el modelo de indexación a partir del texto del issue.
Evaluación
- Stack tecnológico
- python
- Área
- tooling
- Tipo de issue
- Error
- Dificultad
- 3/5
- Tiempo estimado
- 1-2 días
- Estado de actividad
- Estancado
- Claridad
- Bastante claro
- Aptitud para principiantes
- 35/100