KEMBAR78
Incorrect behavior for 'isinstance(x, int) and not isinstance(x, bool)' · Issue #811 · mypyc/mypyc · GitHub
Skip to content

Incorrect behavior for 'isinstance(x, int) and not isinstance(x, bool)' #811

@spenczar

Description

@spenczar

Take this Python module, which has a function to see if a value is an int (but not a bool):

# file: example.py
def is_int(value):
    return isinstance(value, int)

def is_not_bool(value):
    return not isinstance(value, bool)

def is_int_and_not_bool(value):
    return isinstance(value, int) and not isinstance(value, bool)

In pure Python, this acts as I would expect - "True" is a bool, so is_int_and_not_bool(True) returns False:

-> % python -c "import example; print(example.is_int(True))"
True

-> % python -c "import example; print(example.is_not_bool(True))"
False

-> % python -c "import example; print(example.is_int_and_not_bool(True))"
False

If I compile it with mypyc though, the behavior changes. Now, is_int_and_not_bool(True) returns True!

-> % /home/swnelson/code/3p/mypy/scripts/mypyc ./example.py
running build_ext
building 'example' extension
creating build/temp.linux-x86_64-3.9
creating build/temp.linux-x86_64-3.9/build
x86_64-linux-gnu-gcc -pthread -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O2 -Wall -g -fstack-protector-strong -Wformat -Werror=format-security -g -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -fPIC -I/home/swnelson/code/3p/mypy/mypyc/lib-rt -I/home/swnelson/code/personal/avroc/virtualenv3.9/include -I/usr/include/python3.9 -c build/__native.c -o build/temp.linux-x86_64-3.9/build/__native.o -O3 -Werror -Wno-unused-function -Wno-unused-label -Wno-unreachable-code -Wno-unused-variable -Wno-unused-command-line-argument -Wno-unknown-warning-option -Wno-unused-but-set-variable
x86_64-linux-gnu-gcc -pthread -shared -Wl,-O1 -Wl,-Bsymbolic-functions -Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,-Bsymbolic-functions -Wl,-z,relro -g -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 build/temp.linux-x86_64-3.9/build/__native.o -o /tmp/tmp.IkpLWyng8w/example.cpython-39-x86_64-linux-gnu.so

-> % python -c "import example; print(example.is_int(True))"
1

-> % python -c "import example; print(example.is_not_bool(True))"
False

-> % python -c "import example; print(example.is_int_and_not_bool(True))"
True

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions