-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Use checkmember.py to check protocol subtyping #18943
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
base: master
Are you sure you want to change the base?
Changes from all commits
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 |
---|---|---|
|
@@ -4,7 +4,6 @@ | |
from typing import Final, TypeVar, cast, overload | ||
|
||
from mypy.nodes import ARG_STAR, FakeInfo, Var | ||
from mypy.state import state | ||
from mypy.types import ( | ||
ANY_STRATEGY, | ||
AnyType, | ||
|
@@ -544,6 +543,8 @@ def remove_trivial(types: Iterable[Type]) -> list[Type]: | |
* Remove everything else if there is an `object` | ||
* Remove strict duplicate types | ||
""" | ||
from mypy.state import state | ||
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. This nested imports also causes a small performance regression (maybe 0.1% to 0.2%). |
||
|
||
removed_none = False | ||
new_types = [] | ||
all_types = set() | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,16 +4,19 @@ | |
from contextlib import contextmanager | ||
from typing import Final | ||
|
||
from mypy.checker_shared import TypeCheckerSharedApi | ||
|
||
# These are global mutable state. Don't add anything here unless there's a very | ||
# good reason. | ||
|
||
|
||
class StrictOptionalState: | ||
class SubtypeState: | ||
# Wrap this in a class since it's faster that using a module-level attribute. | ||
|
||
def __init__(self, strict_optional: bool) -> None: | ||
# Value varies by file being processed | ||
def __init__(self, strict_optional: bool, type_checker: TypeCheckerSharedApi | None) -> None: | ||
# Values vary by file being processed | ||
self.strict_optional = strict_optional | ||
self.type_checker = type_checker | ||
|
||
@contextmanager | ||
def strict_optional_set(self, value: bool) -> Iterator[None]: | ||
|
@@ -24,6 +27,15 @@ def strict_optional_set(self, value: bool) -> Iterator[None]: | |
finally: | ||
self.strict_optional = saved | ||
|
||
@contextmanager | ||
def type_checker_set(self, value: TypeCheckerSharedApi) -> Iterator[None]: | ||
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. Dependency on 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.
Yeah, I think this should be better. I will play with this. |
||
saved = self.type_checker | ||
self.type_checker = value | ||
try: | ||
yield | ||
finally: | ||
self.type_checker = saved | ||
|
||
|
||
state: Final = StrictOptionalState(strict_optional=True) | ||
state: Final = SubtypeState(strict_optional=True, type_checker=None) | ||
find_occurrences: tuple[str, str] | None = None |
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.
This function does not appear to be a performance bottleneck (at least in self check).
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.
@JukkaL If you will have time, could you please check if there is any slowness because of
bind_self()
andcheck_self_arg()
? Although they are not modified, they may be called much more often now.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.
check_self_arg
could be more expensive -- it appears to consume an extra ~0.5% of runtime in this PR. We are now spending maybe 2-3% of CPU in it, so it's quite hot, but it already was pretty hot before this PR. This could be noise though.I didn't see any major change in
bind_self
when doing self check, though it's pretty hot both before and after, though less hot thancheck_self_arg
.