-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Implement per-file strict Optional #3206
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
Changes from all commits
454844a
61cf01b
e094b8d
8d1623d
0ed98c6
1b5fb11
1df4c7d
d2a4eb6
8e40de0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,13 @@ | ||
from typing import Optional, Tuple | ||
from contextlib import contextmanager | ||
from typing import Optional, Tuple, Iterator | ||
STRICT_OPTIONAL = False | ||
find_occurrences = None # type: Optional[Tuple[str, str]] | ||
|
||
|
||
@contextmanager | ||
def strict_optional_set(value: bool) -> Iterator[None]: | ||
global STRICT_OPTIONAL | ||
saved = STRICT_OPTIONAL | ||
STRICT_OPTIONAL = value | ||
yield | ||
STRICT_OPTIONAL = saved | ||
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. I know you said it was hacky, but this is where I realized quite how hacky... :-) 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. Yep. :/ |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -408,7 +408,7 @@ class HasNone(NamedTuple): | |
x: int | ||
y: Optional[int] = None | ||
|
||
reveal_type(HasNone(1)) # E: Revealed type is 'Tuple[builtins.int, builtins.int, fallback=__main__.HasNone]' | ||
reveal_type(HasNone(1)) # E: Revealed type is 'Tuple[builtins.int, Union[builtins.int, builtins.None], fallback=__main__.HasNone]' | ||
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. Off-topic: I so wish that 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. It is in most cases, actually. Mypy has 2 different ways of rendering types: an internal representation and an external representation. The external representation is what's used for all errors, and renders |
||
|
||
class Parameterized(NamedTuple): | ||
x: int | ||
|
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 wonder if it would be better to wrap the call sites in such a context manager? Fewer lines in the diff, and there's only one call site in build.py (and another in server/update.py which is only used by fine-grained incrementalism). Ditto for check_second_pass() and visit_file() -- these are each only called from one place in build.py and perhaps another place in server/.
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'm not sure how likely it is that we ever add more call sites, but I think that's a bug in the making. The context manager should always wrap this code -- I don't think there's any case where we'd want to call this while ignoring the per-file Strict Optional setting. I understand that you're not a huge fan of code churn, but I don't think it'd be a worthwhile tradeoff in this instance.
Also, FWIW
git blame
has the-w
flag which ignores whitespace changes, so this doesn't have to mess up anyone's blame.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.
Hm. git might have that flag but GitHub doesn't (as shown in this code review).
Maybe you can make it a decorator containing a context manager?
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.
It looks like that won't quite work, but apparently github has a
-w
equivalent too! https://github.com/python/mypy/pull/3206/files?w=1There 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.
Hm, the -w flag is flawed -- it doesn't show review comments, and it's not sticky.