-
-
Notifications
You must be signed in to change notification settings - Fork 122
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
Why does 1 + "foo"
type check?
#52
Comments
Yes, thanks for the report, this is another consequence of the fact that our priority has been on acquiring sound type information to optimize bytecode, not as much on ensuring that we catch all possible runtime type errors. This is sound because we consider the type of the add expression to be dynamic, but ideally we would error here. The odd string representation of the type of |
Thanks for the explanation. To avoid unnecessary engineering effort, we want to process from __future__ import annotations
class MyClass:
n: int
def __init__(self, n: int) -> None:
self.n = n
def __add__(self, c: MyClass) -> MyClass:
return MyClass(self.n + c.n)
def f():
# This doesn't error statically, but does at runtime.
MyClass(1) + "foo"
# This errors statically
MyClass(1).__add__("foo")
# File "add_is_dyn.py", line 16
# MyClass(1).__add__("foo")
# ^
# compiler.static.errors.TypedSyntaxError: type mismatch:
# Exact[str] received for positional arg 'c',
# expected __main__.MyClass |
BTW, does SP ever optimizes integer addition? |
Yes, we intend to support the relationship between operators and their double-underscore methods in the future, but we don't currently.
Not currently. We provide machine primitive integer types ( |
How does SP guarantee that My first guess was that SP would check class definitions at runtime, to rule out things like My second guess is that |
So the typed/untyped code thing is a bit subtle. The meaning of The fact that you can subclass |
Ok, SP protects all its For modeling: what other classes have different meaning in typed & untyped code? (Or, where can we find a list?). I expect all the CTypes are like this. Are there any others? For github: may I open a new issue for |
Only the CTypes: so the various sizes of int and uint, double, cbool... soon I think we'll be adding
Of course, that would be great, thank you! |
6862bbd
2021-10-14
What program did you run?
What happened?
The program passes type checks.
What should have happened?
We expected a compile-time error complaining that we can't add up
int
andstr
.BTW, the type of
int
's__add__
method is printed in an interesting way, what does that type mean?The text was updated successfully, but these errors were encountered: