Skip to content

Add some typing to astroid.inference #1415

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

Merged
merged 8 commits into from
Mar 3, 2022

Conversation

DanielNoord
Copy link
Collaborator

@DanielNoord DanielNoord commented Mar 1, 2022

Steps

  • Write a good description on what the PR does.

Description

The typing on this is incorrect I believe.

https://github.com/PyCQA/astroid/blob/c0d2e5c89a1525e9f500c43b04529628a70e070f/astroid/util.py#L33-L34

I don't think mypy recognises that the decorator on Uninferable makes util.Uninferable an instance instead of the type. I checked the issue tracker and there is no issue about this yet, so I am slightly doubting whether I am in correct in this, but I think this is the correct fix.

Edit: Seeing as pyright does get this right I opened python/mypy#12265.

Type of Changes

Type
🔨 Refactoring

Related Issue

@DanielNoord DanielNoord added the Maintenance Discussion or action around maintaining astroid or the dev workflow label Mar 1, 2022
@DanielNoord DanielNoord requested a review from cdce8p March 1, 2022 09:38
Copy link
Member

@Pierre-Sassoulas Pierre-Sassoulas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thank you @DanielNoord

Copy link
Member

@cdce8p cdce8p left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Regarding the mypy bug. The issue is probably more general. object.__new__ is defined correctly in typeshed, but the decorator is just not applied to the class like it's the case with functions. An example

from typing import Any, Callable

def change_class_to_int(cls: type[object]) -> int:
    return 1

def change_return_to_int(func: Callable[..., Any]) -> int:
    return 2

@change_class_to_int
class A:
    ...

@change_return_to_int
def func() -> None:
    ...

reveal_type(A)
reveal_type(func)

@DanielNoord DanielNoord requested a review from cdce8p March 2, 2022 07:13
Copy link
Member

@cdce8p cdce8p left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might make sense to update the PR title, I think.

@@ -862,14 +862,15 @@ def _do_compare(
return util.Uninferable
# (or both, but "True | False" is basically the same)

assert retval is not None
return retval # it was all the same value
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a type hint to the definition of retval in L840?

@DanielNoord DanielNoord changed the title Fix typing issues related to util.Uninferable Add some typing to astroid.inference Mar 2, 2022
@DanielNoord DanielNoord requested a review from cdce8p March 2, 2022 11:12
@@ -837,7 +843,7 @@ def _do_compare(
>>> _do_compare([1, 3], '<=', [2, 4])
util.Uninferable
"""
retval = None
retval: Union[Literal[None], bool, Type[util.Uninferable]] = None
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
retval: Union[Literal[None], bool, Type[util.Uninferable]] = None
retval: Union[None, bool] = None

Literal[None] is not necessary. Both mypy and pyright understand None.

Additionally, Uninferable is never assigned to retval, only returned directly.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm there a quite a bit of Literal[None]'s in the codebase. Perhaps we should update those at some point.

@cdce8p cdce8p merged commit 3b8db00 into pylint-dev:main Mar 3, 2022
@DanielNoord DanielNoord deleted the typing-issues branch March 3, 2022 11:34
@Pierre-Sassoulas Pierre-Sassoulas modified the milestones: 2.12.0, 2.11.0 Mar 3, 2022
@cdce8p cdce8p mentioned this pull request Mar 3, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Maintenance Discussion or action around maintaining astroid or the dev workflow
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants