gaogaotiantian / objprint

A library that can print Python objects in human readable format
Apache License 2.0
519 stars 43 forks source link

add_objprint and the type checker #106

Closed adrian-herscu closed 2 weeks ago

adrian-herscu commented 3 months ago

When adding add_objprint to a class, it severely interferes with the (pylance) type checker.

gaogaotiantian commented 3 months ago

Could you give me an example?

adrian-herscu commented 3 months ago

Could you give me an example?

from objprint import *

@add_objprint
class Goo:
    f1: int = 2
    f2: str = "kuku"

# ISSUE: pylance error: "Expected 1 more positional argument"
o = Goo()

# ISSUE: f1 is not recognized as a field of o, but printed
print(o.f1)

Both issues disapear if @add_objprint is removed.

Stranger-Jie commented 1 month ago

When adding add_objprint to a class, it severely interferes with the (pylance) type checker.

@adrian-herscu This only happens when pylance extension setting Python->Analysis: Type Checking Mode isn't a default option (off). @gaogaotiantian Simply check the source code find, the main reason cased by typing.Callable for return value.

def add_objprint(
        orig_class: Optional[Type] = None,
        format: str = "string", **kwargs) -> Union[Type, Callable[[Type], Type]]:
        pass

# change
def add_objprint(
        orig_class: Optional[Type] = None,
        format: str = "string", **kwargs) -> Union[Type, Callable]:
        pass

Strong constraints can cause errors in pylance. It can be constraint not hint. Second, python dynamic interpretation may not be able to identify which class it is and what attribute it contains.

gaogaotiantian commented 2 weeks ago

Thanks for the investigation, but the solution is not ideal. Removing [[Type], Type] may fix the error temporarily but it's not the right fix. The problem is the Union and we should overload it. I'll fix it, thanks!

gaogaotiantian commented 2 weeks ago

Fixed in #113