Skip to content

FR: Update objects with monkey patching and/or setattr #2016

Description

@deanm0000

Summary

It would be great if we could update objects and the ty would be aware of those updates. As an example of a real use case, take polars's ability to extend the API. While this feature works great at runtime, it doesn't work with type checking as you can see... https://i.imgur.com/fX4EGpR.png

More generically here's a more minimal example

class Foo:
    def __init__(self, thing:str):
        self.bar=thing


my_foo=Foo("abc")
setattr(my_foo, "abz", "hello") # it'd be great if it would update what it expects of `my_foo` here
my_foo.abz="hello" # this syntax too, if possible

print(my_foo.abz) # As-is it gives, Object of type `Foo` has no attribute `abz`

setattr(Foo, "abz", "hello") # also updating the class itself
Foo.abz="hello" 

print(my_foo.abz) # Same message as above

In rust, this is kind of like implementing your own trait on an imported struct/enum and rust-analyzer knows about the new trait like this

Image

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions