Closed gwk closed 1 month ago
Thanks for reporting the [object Object]
issue. That hasn't been previously reported. I just introduced a fix in pyright, and it will be fixed in the next pyright release.
As for the other issue, this is by design. By default, pyright doesn't mark Union
as deprecated. This is true even in strict mode. I presume that you've opted to enable the deprecateTypingAliases
option in your pyright configuration. Only when this is enabled will you see pyright treat typing.Union
, typing.List
, etc. as deprecated. While none of these older-style typing
special forms are unlikely to go away any time soon, their use does make your code look outdated in general. Many pyright users requested that we provide a way to identify and report their usage to facilitate the modernization of their code base. If you prefer to use older forms like Union
, simply revert the deprecateTypingAliases
configuration option back to its default value of "false".
Eric, thank you for your kind and detailed response. You are correct, I unwittingly checked that box as part of a more involved update to Pylance settings.
I stand by the observation that while typing.List, Dict, and friends are all truly deprecated, Union is not, and as I explained above, not so easily replaced as the others. It is a shame that this is an all-or-nothing setting. I imagine that it is unlikely to change at this point but please consider adding a separate flag for Union that does not misrepresent it as deprecated.
Thanks again for your help!
This issue has been fixed in prerelease version 2024.9.101, which we've just released. You can find the changelog here: CHANGELOG.md
This issue has been fixed in prerelease version 2024.9.101
The release notes state:
Bug fix: Recent pylance is striking out Union imports as deprecated pylance-release#6390
But this is not correct. Optional
and Union
are both still marked as deprecated with Pylance v2024.9.101:
@agkphysics, do you have deprecateTypingAliases
enabled? See this comment above.
Did you turn off the 'deprecateTypingAliases' setting? That's what causes the error to show up for me.
@debonte @rchiodo my hope with this bug report was that deprecateTypingAliases
could be turned on to report all of the truly deprecated types, but not Union
which is still necessary and not actually deprecated.
@gwk, none of these old forms are going away. Removing them from typing
would break too much existing code. That means you can continue to use List
instead of list
, etc. in your code if you prefer. Likewise, you can continue to use Optional[T]
rather than T | None
and Union[S, T]
rather than S | T
. The latter are preferred in modern code bases (and most people find them more readable), but you can stick with the old ones if you want. The deprecateTypingAliases
check was added for users who want to modernize their code bases.
You mentioned that |
isn't a perfect replacement for Union
, but there are ways to make it work for all of the same use cases. For example, let's say that Foo
requires a forward reference and therefore needs to be surrounded by quotes. In this case, "Foo" | int
will not work, but "Foo | int"
will. The need for quoting goes away almost entirely if you use from __future__ import annotations
, which many modern code bases do. Python 3.14 will fully remove the need to include this import.
@erictraut thank you for explaining. Can you clarify what the meaning of the bug fix is in the change log? It vaguely sounds like the intent is to undo "Recent pylance is striking out Union imports as deprecated" but I guess that was just wishful thinking on my part.
The pylance team wrote the release notes, so we'd need to ask them for clarification, but I suspect that it's referring to the fact that I fixed the [object Object]
issue that you reported above. Perhaps their automated scripts just copied the title of your bug report into the release notes. If you look in the pyright release notes, there is no reference to "Union imports".
Perhaps their automated scripts just copied the title of your bug report into the release notes.
Yes, that's correct. We don't write our release notes by hand anymore.
Summary
As of Python 3.12,
typing.Union
is not documented as deprecated. Furthermore, the type|
operator is not a perfect replacement forUnion
, because union can mix string and type arguments whereas|
cannot.Unfortunately, Pylance is reporting that
Union
is deprecated. It also seems to be suffering from a bug where the version number is reported as[object Object]
. Perhaps this is the reason for the error?Environment data
Code Snippet
Expected behavior
I would expect Plyance to not be so aggressive about reporting
typing.Union
as deprecated, when it really is not. It seems that this is somebody's style preference that has been turned into a non-optional warning in the language server. Please consider that the recommended|
type operator is not a perfect replacement forUnion
, because union can mix string and type arguments whereas|
cannot. Pylance should not report it as deprecated or strike it through. At very least, it should display a hint to turn off such aggressive warnings like this in the hover explanation.Note that
Union
is not documented as deprecated in 3.13 RC2 either. See:Actual behavior
Pylance strikes out the
Union
import and displays this message on hover:Logs