Skip to content

No type error when overloading with int+float #410

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

Closed
JukkaL opened this issue Aug 25, 2014 · 4 comments
Closed

No type error when overloading with int+float #410

JukkaL opened this issue Aug 25, 2014 · 4 comments
Labels
bug mypy got something wrong

Comments

@JukkaL
Copy link
Collaborator

JukkaL commented Aug 25, 2014

The line with the first call to f is accepted by the type checker, even though it should be rejected similar to the second call:

from typing import overload

@overload
def f(n: int) -> int:
    return 0

@overload
def f(n: float) -> float:
    return 1.1

f(1) + 'x'   # PROBLEM: No error reported!
f(1.1) + 'x'

I believe that this is due to int being duck type compatible with float.

This is related to #39.

@rawrgulmuffins
Copy link

I'm attempting to write a test case for this in test-overloading.test.

In check-overloading.test I have

[case testOverloadedIntAndFloatPlusString]
from typing import overload

@overload
def f(n: int) -> int:
    return 0

@overload
def f(n: float) -> float:
    return 1.1

f(1) + 'x' # E: Unsupported operand types for + ("float" and "str")
f(1.1) + 'x' # E: Unsupported operand types for + ("float" and "str")

but when I run

python tests.py 

I get

Expected:
  main, line 11: Unsupported operand types for + ("float" and "str") (diff)
  main, line 12: Unsupported operand types for + ("float" and "str") (diff)
Actual:
  main: In function "f":                        (diff)
  main, line 8: Name 'float' is not defined     (diff)

Alignment of first line difference:
  E: main, line 11: Unsupported operand types for + ("float" and "str")
  A: main: In function "f":
         ^
Traceback (most recent call last):
  File "/home/alord/git/mypy/mypy/test/data.py", line 136, in run
    self.perform(self)
  File "/home/alord/git/mypy/mypy/test/testcheck.py", line 77, in run_test
    testcase.file, testcase.line))
  File "/home/alord/git/mypy/mypy/test/helpers.py", line 84, in assert_string_arrays_equal
    raise AssertionFailure(msg)
AssertionFailure: Invalid type checker output (mypy/test/data/check-overloading.test, line 430)

test_check.testOverloadedIntAndFloatPlusString failed

1/2040 test cases failed, 37 skipped.
*** FAILURE ***

I'm unsure if this error is because I wrote my test case correctly or not.

@JukkaL
Copy link
Collaborator Author

JukkaL commented Sep 8, 2014

Ah, the unit tests use minimal stubs for builtins (defined in mypy/test/data/lib-stub/builtins.py and typing.py in the same dir). You can define any classes you need in the test case (instead of using int, float, etc.). Alternatively, you can add the test case to mypy/test/data/pythoneval.test and run it using python3 -m mypy.test.testpythoneval -- here you have full builtins, but these tests take a lot longer to run.

Finally, you can use customized builtins stubs. These live in mypy/test/data/fixtures/*.py. For example, consider mypy/test/data/check-unions.test:

[case testUnion1]
from typing import Union
def f(x: Union[int, str]) -> None:
    if isinstance(x, int):
        y = 1
        y = x
    elif isinstance(x, str):
        z = 'a'
        z = x
[builtins fixtures/isinstance.py]

The final line specifies which builtins to use, in this case mypy/test/data/fixtures/isinstance.py (https://github.com/JukkaL/mypy/blob/master/mypy/test/data/fixtures/isinstance.py). It has a definition of isinstance, which is omitted from the minimal default stubs.

@rawrgulmuffins
Copy link

Is there any documentation that explains how testing works for mypy? if there isn't where do you house development documentation?

@JukkaL
Copy link
Collaborator Author

JukkaL commented Sep 9, 2014

There's some documentation in the wiki (just expanded it a bit):

http://www.mypy-lang.org/wiki/ImplementationOverview

There should be more... Let me know when something is still difficult to figure out, and I'll update the docs -- and of course, anybody can update the documentation ;-)

@JukkaL JukkaL closed this as completed in 58006c5 Nov 8, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong
Projects
None yet
Development

No branches or pull requests

2 participants