-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Infer something for lambda without context #2634
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
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
6f4b530
get some type for lambda without context
elazarg 3d27092
make it typecheck
elazarg 26df5ab
Propagate consequences to main.py
elazarg 4a93269
remove question-comment
elazarg 2f01ec5
silence specific cases
elazarg 4aa53ab
silence, but more similar to before this patch
elazarg c753920
replace ->NoneTyp with ->Void
elazarg 6b4d026
change tests - define `lambda: None` as a procedure
elazarg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,7 +8,7 @@ | |
import sys | ||
import time | ||
|
||
from typing import Any, Dict, List, Mapping, Optional, Set, Tuple | ||
from typing import Any, Dict, List, Mapping, Optional, Set, Tuple, Type, cast | ||
|
||
from mypy import build | ||
from mypy import defaults | ||
|
@@ -128,8 +128,8 @@ def process_options(args: List[str], | |
"""Parse command line arguments.""" | ||
|
||
# Make the help output a little less jarring. | ||
help_factory = (lambda prog: | ||
argparse.RawDescriptionHelpFormatter(prog=prog, max_help_position=28)) | ||
help_factory = cast(Type[argparse.HelpFormatter], (lambda prog: | ||
argparse.RawDescriptionHelpFormatter(prog=prog, max_help_position=28))) | ||
parser = argparse.ArgumentParser(prog='mypy', epilog=FOOTER, | ||
fromfile_prefix_chars='@', | ||
formatter_class=help_factory) | ||
|
@@ -480,7 +480,7 @@ def get_init_file(dir: str) -> Optional[str]: | |
# These two are for backwards compatibility | ||
'silent_imports': bool, | ||
'almost_silent': bool, | ||
} | ||
} # type: Dict[str, Any] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Silence warning in line 562: |
||
|
||
|
||
def parse_config_file(options: Options, filename: str) -> None: | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -577,12 +577,22 @@ list_a = [] # type: List[A] | |
f(list_a, lambda a: a.x) | ||
[builtins fixtures/list.pyi] | ||
|
||
[case testLambdaWithoutContext] | ||
reveal_type(lambda x: x) # E: Revealed type is 'def (x: Any) -> Any' | ||
reveal_type(lambda x: 1) # E: Revealed type is 'def (x: Any) -> builtins.int' | ||
|
||
[case testLambdaContextVararg] | ||
from typing import Callable | ||
def f(t: Callable[[str], str]) -> str: '' | ||
f(lambda *_: '') | ||
|
||
[case testInvalidContextForLambda] | ||
from typing import Callable | ||
f = lambda x: A() # type: Callable[[], A] | ||
f2 = lambda: A() # type: Callable[[A], A] | ||
class A: pass | ||
[out] | ||
main:2: error: Incompatible types in assignment (expression has type Callable[[Any], A], variable has type Callable[[], A]) | ||
main:2: error: Cannot infer type of lambda | ||
main:3: error: Incompatible types in assignment (expression has type Callable[[], A], variable has type Callable[[A], A]) | ||
main:3: error: Cannot infer type of lambda | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perhaps we don't need this error message now? |
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't know how does the shadowing work, but it seems to work (
lambda x: x
does not return the type of an outerx
)