DetachHead / basedpyright

pyright fork with various type checking improvements, improved vscode support and pylance features built into the language server
http://docs.basedpyright.com/
Other
601 stars 12 forks source link

new diagnostic rule - error in fstring if value does not define a `__str__` #453

Open DetachHead opened 1 week ago

DetachHead commented 1 week ago

usually this results in a useless string representation of an object being displayed to the user (eg. <Foo object at 0xasdfasdf>)

KotlinIsland commented 1 week ago

Must apply to print as well, I suggest we action HelpfulString and UnhelpfulString in basedtyping.

You must also consider __repr__ and None

beauxq commented 1 week ago

To clarify:

if value does not define a __str__

Does this mean if both the __str__ and __repr__ we would get is the same as object.__str__ and object.__repr__?

It wouldn't be nice to have to define this in every subclass of a class where we've defined __str__

    def __str__(self) -> str:
        return super().__str__()
DetachHead commented 1 week ago

the motivation is to prevent non-user friendly strings from being presented to the user. i got the idea from this eslint rule.

users who don't like the rule will be able to disable it if they want

KotlinIsland commented 1 week ago

It wouldn't be nice to have to define this in every subclass of a class where we've defined __str__

it would work like this

class A:
    pass
class B(A):
    def __str__(self):
        return "something"
class C(B):
    pass

f"{object()}"  # warning: reportUnhelpfulString
f"{A()}"  # warning: reportUnhelpfulString
f"{B()}"  # no report
f"{C()}"  # no report

If you inherit from a helpful string, then that counts as a helpful string