Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
switch to using FunctionDef
  • Loading branch information
MJoshuaB committed Aug 13, 2024
commit a371656ccd2bcc06a13104c95b828f7dfcc643d0
Original file line number Diff line number Diff line change
Expand Up @@ -2764,13 +2764,14 @@ class DoNotUseLegacyTyping(BaseChecker):
),
}

def visit_functiontype(self, node):
def visit_functiondef(self, node):
"""Check that we aren't using legacy typing."""
self.add_message(
msgid=f"do-not-use-legacy-typing",
node=node,
confidence=None
)
if node.is_function and (node.type_comment_args or node.type_comment_returns):
self.add_message(
msgid=f"do-not-use-legacy-typing",
node=node,
confidence=None,
)

# if a linter is registered in this function then it will be checked with pylint
def register(linter):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4838,17 +4838,45 @@ class TestCheckDoNotUseLegacyTyping(pylint.testutils.CheckerTestCase):

def test_disallowed_typing(self):
"""Check that illegal method typing comments raise warnings"""
fnt = astroid.extract_node(
fdef = astroid.extract_node(
"""
def function(x):
# type: (str) -> str #@
def function(x): #@
# type: (str) -> str
pass
"""
)

with self.assertAddsMessages(
pylint.testutils.MessageTest(
msg_id="do-not-use-legacy-typing",
node=fnt,
line=2,
node=fdef,
col_offset=0,
end_line=2,
end_col_offset=12,
)
):
self.checker.visit_functiontype(fnt)
self.checker.visit_functiondef(fdef)

def test_allowed_typing(self):
"""Check that allowed method typing comments don't raise warnings"""
fdef = astroid.extract_node(
"""
def function(x: str) -> str: #@
pass
"""
)
with self.assertNoMessages():
self.checker.visit_functiondef(fdef)

def test_arbitrary_comments(self):
"""Check that arbitrary comments don't raise warnings"""
fdef = astroid.extract_node(
"""
def function(x): #@
# This is a comment
pass
"""
)
with self.assertNoMessages():
self.checker.visit_functiondef(fdef)