Skip to content

mypy doesn't distinguish between a bool and an int? #14255

Closed as not planned
Closed as not planned
@PedanticHacker

Description

@PedanticHacker

I have encountered a strange mypy error.

My function is defined like this:

def load_settings(table_name: str, key_name: str) -> bool | int:
    """Loads a setting value by the given table_name and key_name."""
    with open(constants.SETTINGS_FILE_PATH, "rb") as settings_file:
        settings = tomllib.load(settings_file)
    return settings[table_name][key_name]

And my variable is defined like this:

is_engine_black: bool = utilities.load_settings("engine", "black")  # Returns: True (so, a bool type)

But the error mypy outputs is this:

mypy: error
assignment - Incompatible types in assignment (expression has type "int", variable has type "bool")

The version of mypy used is 0.991. The operating system used is Windows 11 64-bit (updated to the latest version possible), running on Python 3.11. This error appears in Sublime Text 4 (its latest version of 4143) with the mentioned version of mypy and also in VSCode (also updated to the latest version possible and also updated its mypy extension to the latest version of 0.991 [at the time of writing this]).

So, tell me guys: WTF is going on here?

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrong

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions