Skip to content

Many values trigger warning even when you accept SupportsInt #2136

@sametmax

Description

@sametmax

When linting this program with mypy:


from typing import SupportsInt

class Bar:

    def __int__(self):
        return 1

def foo(a: SupportsInt):
    return int(a)

foo('1')
foo(True)
foo(1)
foo(1.1)
foo(Bar())

I got the following errors:

test.py:12: error: Argument 1 to "foo" has incompatible type "str"; expected "SupportsInt"
test.py:16: error: Argument 1 to "foo" has incompatible type "Bar"; expected "SupportsInt"

I was expecting that anything accepted but int() would be accepted by something hinting for SupportsInt. Is my expectation wrong or is this a bug?

If my expectations are wrong, how would I create the same signature as int()?

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions