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

Handle assert_never() when imported from typing_extensions #9782

Merged
merged 4 commits into from
Jul 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions doc/whatsnew/fragments/9780.false_positive
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Treat `assert_never()` the same way when imported from `typing_extensions`.

Closes #9780
5 changes: 2 additions & 3 deletions pylint/checkers/variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
is_sys_guard,
overridden_method,
)
from pylint.constants import PY311_PLUS, TYPING_NEVER, TYPING_NORETURN
from pylint.constants import TYPING_NEVER, TYPING_NORETURN
from pylint.interfaces import CONTROL_FLOW, HIGH, INFERENCE, INFERENCE_FAILURE
from pylint.typing import MessageDefinitionTuple

Expand Down Expand Up @@ -944,8 +944,7 @@ def _defines_name_raises_or_returns(name: str, node: nodes.NodeNG) -> bool:
if utils.is_terminating_func(node.value):
return True
if (
PY311_PLUS
and isinstance(node.value.func, nodes.Name)
isinstance(node.value.func, nodes.Name)
and node.value.func.name == "assert_never"
):
return True
Expand Down
8 changes: 7 additions & 1 deletion tests/functional/u/used/used_before_assignment_py311.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
"""assert_never() introduced in 3.11"""
from enum import Enum
from typing import assert_never

from pylint.constants import PY311_PLUS

if PY311_PLUS:
Copy link
Member

Choose a reason for hiding this comment

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

I think we should use sys.version_info directly here. Importing from pylint create coupling so more maintenance work when we drop python 3.11 and more importantely code in the wild will never import out internal constants so it makes functional tests less likely to be representative of real code.

Copy link
Member Author

Choose a reason for hiding this comment

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

Why is it Less maintenance?

Copy link
Member

@Pierre-Sassoulas Pierre-Sassoulas Jul 9, 2024

Choose a reason for hiding this comment

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

We'll have to modify this when we drop python 3.11 and the constant does not exists anymore, right ?

Copy link
Member Author

@jacobtylerwalls jacobtylerwalls Jul 9, 2024

Choose a reason for hiding this comment

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

Yes, but I was suggesting it was the same whether we use the constant or the version guard. The last times we dropped versions we also removed version guards.

Copy link
Member

Choose a reason for hiding this comment

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

This is a functional test, I don't think functional test should look like internal pylint code. However it's not really important, let's move on

from typing import assert_never # pylint: disable=no-name-in-module
else:
from typing_extensions import assert_never


class MyEnum(Enum):
Expand Down
2 changes: 0 additions & 2 deletions tests/functional/u/used/used_before_assignment_py311.rc

This file was deleted.

Loading