Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mypy type checking does not work with enums #616

Open
3 tasks done
toby-bro opened this issue Sep 12, 2024 · 2 comments
Open
3 tasks done

Mypy type checking does not work with enums #616

toby-bro opened this issue Sep 12, 2024 · 2 comments
Labels
bug Something isn't working low priority

Comments

@toby-bro
Copy link

toby-bro commented Sep 12, 2024

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

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

$ 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

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: [email protected]
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

  • I have searched the issues for duplicates.
  • I have shown the entire traceback, if possible.
  • I have verified this issue occurs on the latest prelease of betterproto which can be installed using pip install -U --pre betterproto, if possible.
@Gobot1234
Copy link
Collaborator

For now all I can suggest is that you use pyright because its more feature complete and reflects the state of the language far more

@toby-bro
Copy link
Author

toby-bro commented Sep 12, 2024

I made a fixed version of the file but fixed it without properly understanding why we do different things between type checking time and execution time.
toby-bro@87f46fe
As you also did a type checking PR was wondering if it could interest you. I can open a PR on your PR if you want (with the newest version of the file of course).
This solves my problems (as I can't pass from mypy to pyright 😅 )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working low priority
Projects
None yet
Development

No branches or pull requests

2 participants