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

Static type hinting not working in PyCharm or IntelliJ #125

Closed
christianplatta1012 opened this issue Jan 26, 2024 · 6 comments
Closed

Static type hinting not working in PyCharm or IntelliJ #125

christianplatta1012 opened this issue Jan 26, 2024 · 6 comments

Comments

@christianplatta1012
Copy link

Hi,
can you explain, why the type hinting is only working in VC Code and not in PyCharm or IntelliJ?

When I type the following code, "mock_instance" won't get any type hinting in PyCharm or IntelliJ. It works in VS Code.
Is this a configuration problem?

def test_mega_mocking():
patch = MegaPatch.it(ClassICareAbout)
mock_instance = patch.megainstance
mock_instance.hello.return_value = "some value"

Best regards,
Christian

@JamesHutchison
Copy link
Owner

JamesHutchison commented Jan 26, 2024

Hey thanks for bringing this to my attention! I can reproduce your issue and I have a preliminary workaround if you are not using mypy. You can add type hinting in your virtual environment, however mypy will complain about any attributes that aren't shared between the two types.

    @staticmethod
    def it(
        spec: type[T] | None = None,
        *,
        spec_set: bool = True,
        side_effect: Any = None,
        return_value: Any = MISSING,
        _wraps_mock: (
            mock.Mock
            | mock.MagicMock
            | mock.NonCallableMock
            | mock.NonCallableMagicMock
            | None
        ) = None,
        _parent_mega_mock: _MegaMockMixin | None = None,
        **kwargs,
    ) -> T | MegaMock[T, MegaMock | T]:
        ...

I'll need to dig into this more to. Unfortunately Python doesn't have an intersection type, so this functions by doing some hackiness to return a union that VS Code understood to be a union while mypy was interpreting it as an Any. It appears that PyCharm also was using Any

If you're not using mypy and would like this functioning today, you can modify the file inside the virtual environment to add the overload with the return type definition and that should provide the functionality. Here's another example with the_class

    @staticmethod
    def the_class(
        spec: type[T], *, spec_set: bool = True, **kwargs
    ) -> type[T] | MegaMock[type[T], MegaMock[Any, Any] | T]:
        ...

The status of intersection types can be tracked here:

python/typing#213

JamesHutchison added a commit that referenced this issue Jan 26, 2024
* Use no_type_check instead of hack

* Increment version
@JamesHutchison
Copy link
Owner

@christianplatta1012 I pushed version beta-10 which should fix the issue. Please feel free to reopen if it's still not working for you.

pip install --upgrade megamock

@christianplatta1012
Copy link
Author

Hi, unfortunately it is still showing only a generic type "U" in PyCharm and Idea Ultimate. I've created a new project from scratch and installed version 0.1.0b10
megamock_type_pycharm

@christianplatta1012
Copy link
Author

I've wrote some very simplistic test code with the Union Type and this works for IntelliJ:

`from typing import Union,TypeVar

T = TypeVar('T')
def merged_types(x: T) -> Union[str, T]:
if x:
return "x"
else:
return x

def test_union_types():
x= merged_types(ClassICareAbout())`

x autocompletes with all methods from str and ClassICareAbout.
Don't know if this is of any help for finding the problem in megamock though :-/

@JamesHutchison
Copy link
Owner

Thanks for letting me know that it wasn't fully fixed! I was able to reproduce the issue and I just pushed out a new version that fixes that problem. It's worth calling out that if you do something like patch.megainstance.some_method.return_value = 5, the autocompletion won't show up for return_value because the static type checking treats some_method as a function object.

Please LMK if you find any other issues!

@christianplatta1012
Copy link
Author

It works now, thank you for you quick response and help!

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

2 participants