ty icon indicating copy to clipboard operation
ty copied to clipboard

support externally patching methods on instances

Open carljm opened this issue 2 months ago • 2 comments

Initially reported at https://github.com/astral-sh/ty/issues/2134

We should support patching a method on an instance, if the replacement has compatible signature.

https://play.ty.dev/80fd5a68-8c7d-4e73-b5e8-6906a9f9bf61

class RateLimiter:
    def check_limit(self) -> bool:
        return True

def test_lambda_stub_method():
    rl = RateLimiter()

    # Common pattern: stub method with lambda to control return value
    rl.check_limit = lambda: False  # ty: invalid-assignment

carljm avatar Dec 23 '25 21:12 carljm

#350 is also somewhat related

carljm avatar Dec 23 '25 21:12 carljm

I think @sharkdp previously did experiments around this. Possibly in https://github.com/astral-sh/ruff/pull/18275?

AlexWaygood avatar Dec 24 '25 13:12 AlexWaygood