Skip to content

Commit

Permalink
Remove most typing.cast() calls (#4995)
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielNoord committed Sep 15, 2021
1 parent a3f3405 commit cb89612
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 17 deletions.
6 changes: 1 addition & 5 deletions pylint/checkers/classes.py
Expand Up @@ -47,7 +47,7 @@
"""
import collections
from itertools import chain, zip_longest
from typing import List, Pattern, cast
from typing import List, Pattern

import astroid
from astroid import nodes
Expand Down Expand Up @@ -907,7 +907,6 @@ def leave_classdef(self, node: nodes.ClassDef) -> None:

def _check_unused_private_functions(self, node: nodes.ClassDef) -> None:
for function_def in node.nodes_of_class(nodes.FunctionDef):
function_def = cast(nodes.FunctionDef, function_def)
if not is_attr_private(function_def.name):
continue
parent_scope = function_def.parent.scope()
Expand All @@ -918,7 +917,6 @@ def _check_unused_private_functions(self, node: nodes.ClassDef) -> None:
):
continue
for attribute in node.nodes_of_class(nodes.Attribute):
attribute = cast(nodes.Attribute, attribute)
if (
attribute.attrname != function_def.name
or attribute.scope() == function_def # We ignore recursive calls
Expand Down Expand Up @@ -978,7 +976,6 @@ def _check_unused_private_variables(self, node: nodes.ClassDef) -> None:

def _check_unused_private_attributes(self, node: nodes.ClassDef) -> None:
for assign_attr in node.nodes_of_class(nodes.AssignAttr):
assign_attr = cast(nodes.AssignAttr, assign_attr)
if not is_attr_private(assign_attr.attrname) or not isinstance(
assign_attr.expr, nodes.Name
):
Expand All @@ -999,7 +996,6 @@ def _check_unused_private_attributes(self, node: nodes.ClassDef) -> None:
)

for attribute in node.nodes_of_class(nodes.Attribute):
attribute = cast(nodes.Attribute, attribute)
if attribute.attrname != assign_attr.attrname:
continue

Expand Down
10 changes: 1 addition & 9 deletions pylint/checkers/refactoring/recommendation_checker.py
@@ -1,6 +1,6 @@
# Licensed under the GPL: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
# For details: https://github.com/PyCQA/pylint/blob/main/LICENSE
from typing import Union, cast
from typing import Union

import astroid
from astroid import nodes
Expand Down Expand Up @@ -128,17 +128,14 @@ def _check_use_maxsplit_arg(self, node: nodes.Call) -> None:
# Check if loop present within the scope of the node
scope = node.scope()
for loop_node in scope.nodes_of_class((nodes.For, nodes.While)):
loop_node = cast(nodes.NodeNG, loop_node)
if not loop_node.parent_of(node):
continue

# Check if var is mutated within loop (Assign/AugAssign)
for assignment_node in loop_node.nodes_of_class(nodes.AugAssign):
assignment_node = cast(nodes.AugAssign, assignment_node)
if node.parent.slice.name == assignment_node.target.name:
return
for assignment_node in loop_node.nodes_of_class(nodes.Assign):
assignment_node = cast(nodes.Assign, assignment_node)
if node.parent.slice.name in [
n.name for n in assignment_node.targets
]:
Expand Down Expand Up @@ -216,7 +213,6 @@ def _check_consider_using_enumerate(self, node: nodes.For) -> None:
# for body.
for child in node.body:
for subscript in child.nodes_of_class(nodes.Subscript):
subscript = cast(nodes.Subscript, subscript)
if not isinstance(subscript.value, expected_subscript_val_type):
continue

Expand Down Expand Up @@ -254,8 +250,6 @@ def _check_consider_using_dict_items(self, node: nodes.For) -> None:
# for body.
for child in node.body:
for subscript in child.nodes_of_class(nodes.Subscript):
subscript = cast(nodes.Subscript, subscript)

if not isinstance(subscript.value, (nodes.Name, nodes.Attribute)):
continue

Expand Down Expand Up @@ -304,8 +298,6 @@ def _check_consider_using_dict_items_comprehension(

for child in node.parent.get_children():
for subscript in child.nodes_of_class(nodes.Subscript):
subscript = cast(nodes.Subscript, subscript)

if not isinstance(subscript.value, (nodes.Name, nodes.Attribute)):
continue

Expand Down
4 changes: 1 addition & 3 deletions pylint/checkers/refactoring/refactoring_checker.py
Expand Up @@ -6,7 +6,7 @@
import itertools
import tokenize
from functools import reduce
from typing import Dict, Iterator, List, NamedTuple, Optional, Tuple, Union, cast
from typing import Dict, Iterator, List, NamedTuple, Optional, Tuple, Union

import astroid
from astroid import nodes
Expand Down Expand Up @@ -1862,8 +1862,6 @@ def _check_unnecessary_dict_index_lookup(
)
for child in children:
for subscript in child.nodes_of_class(nodes.Subscript):
subscript = cast(nodes.Subscript, subscript)

if not isinstance(subscript.value, (nodes.Name, nodes.Attribute)):
continue

Expand Down

0 comments on commit cb89612

Please sign in to comment.