-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
Remove, move or # noqa
more TypeAlias
declarations
#8450
Changes from 1 commit
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,5 +1,5 @@ | ||
import sys | ||
from multiprocessing import context, reduction as reducer, synchronize | ||
from multiprocessing import context, reduction as reducer | ||
from multiprocessing.context import ( | ||
AuthenticationError as AuthenticationError, | ||
BufferTooShort as BufferTooShort, | ||
|
@@ -10,12 +10,10 @@ from multiprocessing.context import ( | |
from multiprocessing.process import active_children as active_children, current_process as current_process | ||
|
||
# These are technically functions that return instances of these Queue classes. | ||
# Using them as annotations is deprecated. Either use imports from | ||
# multiprocessing.queues or the aliases defined below. See #4266 for discussion. | ||
# Using them as annotations is deprecated. Use imports from multiprocessing.queues instead. | ||
# See #4266 for discussion. | ||
AlexWaygood marked this conversation as resolved.
Show resolved
Hide resolved
|
||
from multiprocessing.queues import JoinableQueue as JoinableQueue, Queue as Queue, SimpleQueue as SimpleQueue | ||
from multiprocessing.spawn import freeze_support as freeze_support | ||
from typing import TypeVar | ||
from typing_extensions import TypeAlias | ||
|
||
if sys.version_info >= (3, 8): | ||
from multiprocessing.process import parent_process as parent_process | ||
|
@@ -62,27 +60,6 @@ __all__ = [ | |
if sys.version_info >= (3, 8): | ||
__all__ += ["parent_process"] | ||
|
||
# The following type aliases can be used to annotate the return values of | ||
# the corresponding functions. They are not defined at runtime. | ||
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. 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. Ah, thanks for the reference. 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. The other options are:
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 think this is somewhat of a "true positive". These type aliases are just weird: named as if they were private, intended to be public, not documented, not discoverable. But again, @srittau will probably disagree. I think we should wait for his input. 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 of course, not going to merge without it :) |
||
# | ||
# from multiprocessing import Lock | ||
# from typing import TYPE_CHECKING | ||
# if TYPE_CHECKING: | ||
# from multiprocessing import _LockType | ||
# lock: _LockType = Lock() | ||
|
||
_T = TypeVar("_T") | ||
_QueueType: TypeAlias = Queue[_T] | ||
_SimpleQueueType: TypeAlias = SimpleQueue[_T] | ||
_JoinableQueueType: TypeAlias = JoinableQueue[_T] | ||
_BarrierType: TypeAlias = synchronize.Barrier | ||
_BoundedSemaphoreType: TypeAlias = synchronize.BoundedSemaphore | ||
_ConditionType: TypeAlias = synchronize.Condition | ||
_EventType: TypeAlias = synchronize.Event | ||
_LockType: TypeAlias = synchronize.Lock | ||
_RLockType: TypeAlias = synchronize.RLock | ||
_SemaphoreType: TypeAlias = synchronize.Semaphore | ||
|
||
# These functions (really bound methods) | ||
# are all autogenerated at runtime here: https://github.com/python/cpython/blob/600c65c094b0b48704d8ec2416930648052ba715/Lib/multiprocessing/__init__.py#L23 | ||
RawValue = context._default_context.RawValue | ||
|
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 think this comment is just outdated? As far as I can see, all these are classes in the implementation, at least in Python 3.7+. Was this maybe different in the past? In that case, I don't think we need the type aliases below.
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.
They became functions in the stub at some point, but it was reverted.
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.
Somehow I missed that you asked about the implementation, not about the stub. These are functions (actually methods) in the implementation:
There is also an undocumented class that is different from the public and documented
Queue
:There's similarly e.g. a method
multiprocessing.Event
and a classmultiprocessing.synchronize.Event
.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 is what happens at runtime:
So the stubs are a bit of a lie at the moment. As @Akuli says, the stubs were changed in #4289 so that they could match the runtime more closely, but then that was reverted in #4314 after Jukka complained in #4313.
The thing is, either way, I don't think having these aliases helps the situation much. To work around these issues, a user can probably just put
multiprocessing.Queue
in quotes in their annotations, or usefrom __future__ import annotations
.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.
We should try to make them into functions again in the stubs and see the fallout, now that we have mypy_primer. Annotating something with
muliprocessing.Queue
, which is a function, is not something we should support.This comment was marked as off-topic.
Sorry, something went wrong.
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 marked my previous comment as off-topic, as it doesn't belong at all to this PR. I somehow didn't see other people's comments that were posted before I wrote mine.
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 really see how the aliases help, even the ones which are alternatives to a function.
Take
multiprocessing.Lock
--multiprocessing.Lock
is a method, butmultiprocessing.synchronize.Lock
is a class. If it's a choice between doing this:or this:
Then, I find the first way much cleaner and more ergonomic. And, as @Akuli says in his minimized comment, we know none of the projects in mypy_primer are using these aliases.
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.
Sorry for being unclear. Yes, if there is a valid type somewhere, like
multiprocessing.synchronize.Lock
, we don't need the alias. It's possible that this is true for all aliases, I didn't 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.
_BarrierType
,_BoundedSemaphoreType
,_ConditionType
,_EventType
,_LockType
,_RLockType
and_SemaphoreType
are all aliases to valid types inmultiprocessing.synchronize
._QueueType
,_SimpleQueueType
and_JoinableQueueType
are all aliases to valid types inmultiprocessing.queues
.So yeah, it's true for all the aliases :)