Skip to content

Commit b42e574

Browse files
authored
Reduce monkeypatching in pyflakes checker (#372)
Chipping away at #183 Got rid of the monkeypatched `LAMBDA`. What's changed -> - Default argument values cannot be forward references anymore: ```python def bar(x: type = SomeClass) -> None: ... # F821 undefined name 'SomeClass' class SomeClass: ... ``` I think it makes sense to not allow this anyway - it's similar to what we did with classes previously.
1 parent 80d3cba commit b42e574

File tree

3 files changed

+20
-11
lines changed

3 files changed

+20
-11
lines changed

CHANGELOG.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
11
# Change Log
22

3+
## Unreleased
4+
5+
* The way in which flake8-pyi modifies pyflakes runs has been improved. When
6+
flake8-pyi is installed, pyflakes will now complain about forward references
7+
in default values for function and method parameters (the same as pyflakes
8+
does when it checks `.py` files). Unlike in `.py` files, forward references
9+
in default values are legal in stub files. However, they are never necessary,
10+
and are considered bad style. (Forward references for parameter *annotations*
11+
are still allowed.)
12+
13+
Contributed by [tomasr8](https://github.com/tomasr8).
14+
315
## 23.5.0
416

517
* flake8-pyi no longer supports being run with flake8 <5.0.4.

pyi.py

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -216,17 +216,6 @@ def ASSIGN(
216216

217217
self.deferHandleNode(tree.value, tree)
218218

219-
def LAMBDA(self, node: ast.Lambda) -> None:
220-
"""This is likely very brittle, currently works for pyflakes 1.3.0.
221-
222-
Deferring annotation handling depends on the fact that during calls
223-
to LAMBDA visiting the function's body is already deferred and the
224-
only eager calls to `handleNode` are for annotations.
225-
"""
226-
self.handleNode, self.deferHandleNode = self.deferHandleNode, self.handleNode # type: ignore[method-assign]
227-
super().LAMBDA(node)
228-
self.handleNode, self.deferHandleNode = self.deferHandleNode, self.handleNode # type: ignore[method-assign]
229-
230219
def handleNodeDelete(self, node: ast.AST) -> None:
231220
"""Null implementation.
232221

tests/forward_refs.pyi

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,11 @@ class C:
2121
class Outer:
2222
class Inner1: ...
2323
class Inner2(list[Outer.Inner1]): ...
24+
25+
# Allow forward references for argument annotations and the return type
26+
def foo(x: SomeClass) -> SomeClass: ...
27+
class SomeClass: ...
28+
29+
# Disallow forward references for default argument values
30+
def bar(x: type = SomeOtherClass) -> None: ... # Y011 Only simple default values allowed for typed arguments # F821 undefined name 'SomeOtherClass'
31+
class SomeOtherClass: ...

0 commit comments

Comments
 (0)