Files
ruff/crates/red_knot_python_semantic/resources/mdtest/assignment/augmented.md
David Peter 53fa32a389 [red-knot] Remove Type::Unbound (#13980)
<!--
Thank you for contributing to Ruff! To help us out with reviewing,
please consider the following:

- Does this pull request include a summary of the change? (See below.)
- Does this pull request include a descriptive title?
- Does this pull request include references to any relevant issues?
-->

## Summary

- Remove `Type::Unbound`
- Handle (potential) unboundness as a concept orthogonal to the type
system (see new `Symbol` type)
- Improve existing and add new diagnostics related to (potential)
unboundness

closes #13671 

## Test Plan

- Update existing markdown-based tests
- Add new tests for added/modified functionality
2024-10-31 20:05:53 +01:00

2.5 KiB

Augmented assignment

Basic

x = 3
x -= 1
reveal_type(x)  # revealed: Literal[2]

x = 1.0
x /= 2
reveal_type(x)  # revealed: float

Dunder methods

class C:
    def __isub__(self, other: int) -> str:
        return "Hello, world!"

x = C()
x -= 1
reveal_type(x)  # revealed: str

class C:
    def __iadd__(self, other: str) -> float:
        return 1.0

x = C()
x += "Hello"
reveal_type(x)  # revealed: float

Unsupported types

class C:
    def __isub__(self, other: str) -> int:
        return 42

x = C()
x -= 1

# TODO: should error, once operand type check is implemented
reveal_type(x)  # revealed: int

Method union

def bool_instance() -> bool:
    return True

flag = bool_instance()

class Foo:
    if bool_instance():
        def __iadd__(self, other: int) -> str:
            return "Hello, world!"
    else:
        def __iadd__(self, other: int) -> int:
            return 42

f = Foo()
f += 12

reveal_type(f)  # revealed: str | int

Partially bound __iadd__

def bool_instance() -> bool:
    return True

class Foo:
    if bool_instance():
        def __iadd__(self, other: str) -> int:
            return 42

f = Foo()

# TODO: We should emit an `unsupported-operator` error here, possibly with the information
# that `Foo.__iadd__` may be unbound as additional context.
f += "Hello, world!"

reveal_type(f)  # revealed: int

Partially bound with __add__

def bool_instance() -> bool:
    return True

class Foo:
    def __add__(self, other: str) -> str:
        return "Hello, world!"
    if bool_instance():
        def __iadd__(self, other: str) -> int:
            return 42

f = Foo()
f += "Hello, world!"

# TODO(charlie): This should be `int | str`, since `__iadd__` may be unbound.
reveal_type(f)  # revealed: int

Partially bound target union

def bool_instance() -> bool:
    return True

class Foo:
    def __add__(self, other: int) -> str:
        return "Hello, world!"
    if bool_instance():
        def __iadd__(self, other: int) -> int:
            return 42

if bool_instance():
    f = Foo()
else:
    f = 42.0
f += 12

# TODO(charlie): This should be `str | int | float`
reveal_type(f)  # revealed: @Todo

Target union

def bool_instance() -> bool:
    return True

flag = bool_instance()

class Foo:
    def __iadd__(self, other: int) -> str:
        return "Hello, world!"

if flag:
    f = Foo()
else:
    f = 42.0
f += 12

# TODO(charlie): This should be `str | float`.
reveal_type(f)  # revealed: @Todo