Why does `1 + "foo"` type check?

Open
#52 8 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
35/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Stale
Tech stack
python
Domain
compilers

Research direction

Start by running the provided Python program through the type checker and inspect the reported type for (42).__add__ with reveal_type. Trace how the checker handles int and str operands and the displayed int.__add__ type. Done means the incompatible addition produces a compile-time error and the method type is explained or corrected.

Written by the indexing model from the issue text.

Description

sp-ux staticpython

6862bbd
2021-10-14

What program did you run?
def f():
    return 1 + "foo"

f()
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 and str.

BTW, the type of int's __add__ method is printed in an interesting way, what does that type mean?

reveal_type((42).__add__)
# compiler.static.errors.TypedSyntaxError:
# reveal_type(int(42).__add__):
# 'int.__add__'
Dominant language
Python
Stars
3.8k
Forks
139
PR merge metrics
No merged PRs in 30d

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from facebookincubator/MetaPython

All issues in facebookincubator/MetaPython

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.