Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add_objprint and the type checker #106

Closed
adrian-herscu opened this issue Jul 31, 2024 · 5 comments
Closed

add_objprint and the type checker #106

adrian-herscu opened this issue Jul 31, 2024 · 5 comments

Comments

@adrian-herscu
Copy link

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

@gaogaotiantian
Copy link
Owner

Could you give me an example?

@adrian-herscu
Copy link
Author

adrian-herscu commented Aug 1, 2024

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
Copy link

Stranger-Jie commented Sep 13, 2024

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
Copy link
Owner

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
Copy link
Owner

Fixed in #113

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants