Skip to content

Add support for conditionally defined overloads #10712

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 28 commits into from
Mar 3, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
99436ee
Add support for conditionally defined overloads
cdce8p Jun 25, 2021
7e7502b
Bugfix
cdce8p Jun 25, 2021
52ba893
Merge remote-tracking branch 'upstream/master' into conditional-overl…
cdce8p Dec 14, 2021
3effa15
Redo logic to support elif + else
cdce8p Dec 14, 2021
ee6ad3c
Fix typing issues
cdce8p Dec 14, 2021
bc486e0
Fix small issue with merging IfStmt
cdce8p Dec 15, 2021
a1370e0
Update existing tests
cdce8p Dec 15, 2021
0d2dee3
Add additional tests
cdce8p Dec 15, 2021
ae394cc
Fix check-functions tests
cdce8p Dec 15, 2021
1dd5679
Fix crash
cdce8p Dec 15, 2021
a8c3899
Remove redundant cast
cdce8p Dec 15, 2021
3a93f7e
Add last test cases
cdce8p Dec 15, 2021
9a4c703
Fix tests
cdce8p Dec 15, 2021
2777ce1
Typecheck skipped IfStmt conditions
cdce8p Dec 15, 2021
b36a5f8
More tests
cdce8p Dec 15, 2021
4c2e98b
Merge remote-tracking branch 'upstream/master' into conditional-overl…
cdce8p Dec 15, 2021
1452020
Merge remote-tracking branch 'upstream/master' into conditional-overl…
cdce8p Dec 16, 2021
dcc49b2
Merge remote-tracking branch 'upstream/master' into conditional-overl…
cdce8p Jan 10, 2022
f8e27b2
Merge remote-tracking branch 'upstream/master' into conditional-overl…
cdce8p Jan 18, 2022
14defb4
Merge remote-tracking branch 'upstream/master' into conditional-overl…
cdce8p Jan 30, 2022
d6cc690
Merge remote-tracking branch 'upstream/master' into conditional-overl…
cdce8p Feb 6, 2022
7cb5eac
Merge remote-tracking branch 'upstream/master' into conditional-overl…
cdce8p Feb 25, 2022
80b05d5
Apply suggestions from review
cdce8p Mar 2, 2022
3d0397a
Don't merge starting If blocks without overloads
cdce8p Mar 2, 2022
914d517
Emit error if condition can't be inferred
cdce8p Mar 2, 2022
4f79d43
Add additional test cases
cdce8p Mar 2, 2022
4776070
Add documentation
cdce8p Mar 2, 2022
cd1e9b0
Copyedits to the docs
JelleZijlstra Mar 3, 2022
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
Don't merge starting If blocks without overloads
  • Loading branch information
cdce8p committed Mar 2, 2022
commit 3d0397a62aba7f36154391f18075059bd63b6cba
6 changes: 5 additions & 1 deletion mypy/fastparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,11 @@ def fix_function_overloads(self, stmts: List[Statement]) -> List[Statement]:
if (
isinstance(stmt, IfStmt)
and len(stmt.body[0].body) == 1
and isinstance(stmt.body[0].body[0], (Decorator, FuncDef, OverloadedFuncDef))
and (
isinstance(stmt.body[0].body[0], (Decorator, OverloadedFuncDef))
or current_overload_name is not None
and isinstance(stmt.body[0].body[0], FuncDef)
)
):
# Check IfStmt block to determine if function overloads can be merged
if_overload_name = self._check_ifstmt_for_overloads(stmt)
Expand Down
6 changes: 2 additions & 4 deletions test-data/unit/check-functions.test
Original file line number Diff line number Diff line change
Expand Up @@ -1400,9 +1400,8 @@ def top() -> None:
from typing import Any
x = None # type: Any
if x:
pass # some other node
def f(): pass
def f(): pass # E: Name "f" already defined on line 5
def f(): pass # E: Name "f" already defined on line 4

[case testIncompatibleConditionalFunctionDefinition]
from typing import Any
Expand Down Expand Up @@ -1647,9 +1646,8 @@ from typing import Any
x = None # type: Any
class A:
if x:
pass # Some other node
def f(self): pass
def f(self): pass # E: Name "f" already defined on line 6
def f(self): pass # E: Name "f" already defined on line 5

[case testIncompatibleConditionalMethodDefinition]
from typing import Any
Expand Down
6 changes: 3 additions & 3 deletions test-data/unit/check-overloading.test
Original file line number Diff line number Diff line change
Expand Up @@ -6153,11 +6153,11 @@ reveal_type(f5(A())) # N: Revealed type is "__main__.A"
reveal_type(f5(B())) # N: Revealed type is "__main__.B"

# Test from check-functions - testUnconditionalRedefinitionOfConditionalFunction
# If IfStmt only contains FuncDef, block is ignore if uncertain about execution
# Necessary to be able to ignore always-false cases
# Don't merge If blocks if they appear before any overloads
# and don't contain any overloads themselves.
if maybe_true: # E: Name "maybe_true" is not defined
def f6(x): ...
def f6(x): ...
def f6(x): ... # E: Name "f6" already defined on line 61

if maybe_true: # E: Name "maybe_true" is not defined
pass # Some other node
Expand Down