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

bool/int substitutions are valid only sometimes: x, y = False, True # type int, int #1757

Closed
cjwelborn opened this issue Jun 28, 2016 · 5 comments

Comments

@cjwelborn
Copy link

cjwelborn commented Jun 28, 2016

Like the title says, this is okay in mypy:

mypy -c 'x, y = False, True  # type: int, int'

But the reverse is not:

mypy -c 'x, y = 1, 0  # type: bool, bool'
<string>:1: error: Incompatible types in assignment (expression has type "int", variable has type "bool")

I first found it checking to see if mypy cares about bool/int where Python itself doesn't.
Python is okay with this because bool sub-classes int, and mixed arithmetic is okay:

(False, 0.0, 0, 1).count(False)
>> 3
[2, 1].index(True)
>> 1

mypy doesn't seem to care about this:

mypy -c 'l = [False, True, 0, 1]  # type: List[int]'

..but it cares about the opposite:

mypy -c 'l = [False, True, 0, 1]  # type: List[bool]'
<string>:1: error: List item 2 has incompatible type "int"
<string>:1: error: List item 3 has incompatible type "int"

Is this an actual bug, or is this documented somewhere? I know why Python does it, I just thought that mypy would say something about it.

@cjwelborn cjwelborn changed the title Incorrect types are valid: x, y = False, True # type int, int bool/int substitutions are valid sometimes: x, y = False, True # type int, int Jun 28, 2016
@cjwelborn cjwelborn changed the title bool/int substitutions are valid sometimes: x, y = False, True # type int, int bool/int substitutions are valid only sometimes: x, y = False, True # type int, int Jun 28, 2016
@refi64
Copy link
Contributor

refi64 commented Jun 28, 2016

I don't think this is a bug. In Python, because bool subclasses int, but not the other way around. For instance:

x = True
print(isinstance(x, int)) # True
x = 0
print(isinstance(x, bool)) # False

@gvanrossum
Copy link
Member

Indeed, I think the OP is confused by Python's accepting if 0: ... -- but that's not because ints and bools are interchangeable; it's because if doesn't require a bool at all, it takes any object and applies a "truth-finding" dunder method to it (__bool__ or __len__).

@cjwelborn
Copy link
Author

cjwelborn commented Jun 28, 2016

I can see the reasoning, that because bool is technically an int it type checks. I just thought that mypy would be a little stricter where python isn't. I wasn't really talking about __bool__ or if statements.

I was talking about how in python, these examples are equivalent:

sum([False, True, True])
sum([0, 1, 1])

x = ['a', 'b'][False]
x = ['a', 'b'][0]

(0.0, 0, False).count(0.0)
(0.0, 0, False).count(0)
(0.0, 0, False).count(False)

I just thought mypy would be a little stricter, and say that x = False # type: int is an error. No big deal though, my question was answered.

@0ion9
Copy link

0ion9 commented Jun 30, 2016

@cjwelborn people aren't notified of comments after an issue is closed. If you want them to notice you have to @mention them like I did in this message.

@cjwelborn
Copy link
Author

@0ion9, thanks. I should have done that.

mateuszmandera added a commit to shanukun/zulip that referenced this issue Aug 1, 2021
An integer or no argument is supposed to be passed.
These weren't caught by mypy because booleans are integers in python,
see python/mypy#1757
mateuszmandera added a commit to shanukun/zulip that referenced this issue Aug 2, 2021
An integer or no argument is supposed to be passed.
These weren't caught by mypy because booleans are integers in python,
see python/mypy#1757
timabbott pushed a commit to shanukun/zulip that referenced this issue Sep 7, 2021
An integer or no argument is supposed to be passed.
These weren't caught by mypy because booleans are integers in python,
see python/mypy#1757
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants