-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Very slow performance for huge literals #7143
Comments
Pyright already contains significant optimizations for large unions, but I've never seen a union with more than a few hundred items. The one in your example contains nearly 3500 elements. Many type operations involving unions are O(n^2), and if those operations are nested within other operations, the cost can grow many times that. I'll take a look at whether there are additional optimizations that could help here, but I consider this a low priority. I also don't consider this a bug, so I'll change it to an enhancement request. |
Thanks! Just note that even five hundred items make pyright take 3 seconds more to run on my project. |
The reason I created it as a bug is because this issue makes pyright's autocompletion basically unusable for a project having a single file like this in its codebase. I think having a literal with 3500 items is as legit as having a literal with 35 items, it shouldn't break the tool. |
TS and Python are different languages, and their type systems differ somewhat, so direct comparisons don't necessarily apply. I recommend against creating such large unions. There are many tools in the Python ecosystem that will not be able to deal with them. I know from experience that pyright already deals with large unions much better than mypy, and both of them deal better with large unions that the other two major type checkers (pyre and pytype). I haven't tested runtime type checkers or libraries that use types at runtime, but I would guess that they would also perform very badly if they encountered such a large union.
I'm going to push back on you there. There will be some limit beyond which performance will be unacceptably slow. You're creating a type here that is far larger (by an order of magnitude!) than what I have seen before in Python code. |
Yes, but I really doubt if pyright needs to do anything in complexity of O(n^2) just cause it is dealing with Python and tsserver doesn't need to do that because it is dealing with TypeScript. I mean doing a prefix search on a huge list of strings shouldn't be much different when it is used for TypeScript autocompletion or Python autocompletion. But you definitely know better.
I understand that it is not a common use-case, but I don't agree it is a reason to conclude it's not a legit use-case. The gist I provided is a good example itself: using literal unions for the list of material icons isn't abusing the Python's annotation api to achieve something it is not designed for. Maybe it's not common because people try it and see Python tools are not working well with large unions and they don't bother creating issues in their repositories and just give up and go with normal strings. But it's obvious that if we can benefit from annotations here, it will reduce human error and is considered an improvement for this tool and the Python community. It has happened to myself many times in the past few weeks that I mistakenly wrote an icon name with dashes while it needed underscore or write it plural while it was singular or had a typo. |
I've tried playing with this locally (both in VSCode and by running |
…ns of literal types. This addresses #7143.
As I mentioned, pyright already contains numerous optimizations specifically for unions that contain many literals. I've added special-casing to a few more code paths, which significantly decreases the analysis time in this particular example — at least for some common use cases. These optimizations will be in the next release. That said, I stand by my earlier statement. I strongly recommend against defining such large unions. There are many situations where a Python type checker must check all combinations of two unions, which results in O(n x m) performance. Take for example: def func(a: Icon, b: Icon | None):
return a + b In Python (unlike in TypeScript), the If you define unions with thousands of subtypes, you will encounter these "performance cliffs" in a type checker. When you do, I will not be sympathetic. I've confirmed that mypy's behavior is similar, which is unsurprising. Please don't abuse the type system! |
Doing a prefix search in even 60,000 items shouldn't take long, if the underlying data-structure is working in I'm not abusing typing system, Pyright gives up so fast: I understand there is a lot of work to do and Pyright is mostly a one man show and it is an open source project, so I'm not saying we need to push the limit of the number of items in a literal that makes Pyright unresponsive from 200 to 10K in the next version. So I think I'm not abusing the typing system, the typing system and tools like Pyright are being developed and they are not mature enough to handle all the legit use-cases like my legit use-case of creating a literal for names of the icons in an icon font. |
This is addressed in pyright 1.1.349, which I just published. |
Thank you! The autocompletion is tens of times faster (it responds in less than 200ms) and pyright checks the whole codebase in less than 3 seconds (it used to take more than 20 seconds.) |
commit 7c060cb Author: Eric Traut <eric@traut.com> Date: Fri Feb 2 12:58:09 2024 -0800 Another tweak to improve interruptions. commit e5f13bb Author: Eric Traut <eric@traut.com> Date: Fri Feb 2 00:56:00 2024 -0800 Fixed a bug that results in a false positive error when a TypeVar bound to a union of literals is used in the specialization of a TypeAlias whose TypeVar is bound to a wider union of literals. This addresses microsoft#7184. (microsoft#7187) commit 42d853f Author: Eric Traut <eric@traut.com> Date: Wed Jan 31 20:50:15 2024 -0800 Fixed a bug that results in a false negative when a `None` type is in… (microsoft#7176) * Fixed a bug that results in a false negative when a `None` type is included in an unpacked argument within a function call. This addresses microsoft#7175. * Improved logic for unpacked arguments that contain an "Unbound" type. commit a81633d Author: Eric Traut <eric@traut.com> Date: Wed Jan 31 16:59:53 2024 -0800 Fixed a bug that resulted in an incorrect type evaluation when a TypeVar with a default (PEP 696) was used in an overload but was not solved. This addresses microsoft#7173. (microsoft#7174) commit e01b0fe Author: Rich Chiodo <rchiodo@users.noreply.github.com> Date: Wed Jan 31 16:38:37 2024 -0800 Add test(s) that validate Pyright can talk over LSP (microsoft#7172) * Everything building, but not running * More tests passing * Fix test to open a file * Remove unused functions and consolidate others * Remove unused custom lsp messages * Add comments commit 7585378 Author: Eric Traut <eric@traut.com> Date: Tue Jan 30 21:04:24 2024 -0800 Changed behavior of `super()` method call when `self` is annotated as a protocol class. This pattern is used for annotating mix-ins. In this case, pyright should not generate an error if the protocol's method isn't implemented. This addresses microsoft#7160. (microsoft#7168) commit 8af045b Author: Erik De Bonte <erikd@microsoft.com> Date: Tue Jan 30 18:39:58 2024 -0800 Grant write permissions as needed to workflows that use GITHUB_TOKEN (microsoft#7165) commit 12e9dd9 Author: seairth <Seairth@users.noreply.github.com> Date: Mon Jan 29 12:48:49 2024 -0500 Added multi-root workspaceFolder support in path variable expansion (microsoft#7138) Co-authored-by: Seairth Jacobs <sjacobs@wrsystems.com> commit 419ae42 Author: Eric Traut <eric@traut.com> Date: Sun Jan 28 23:37:23 2024 -0800 Published 1.1.349 commit 6457531 Author: Eric Traut <eric@traut.com> Date: Sun Jan 28 23:31:20 2024 -0800 Fixed a bug that resulted in incorrect type narrowing for sequence patterns when the subject expression contains a tuple with an unbounded component. This addresses microsoft#7117. (microsoft#7156) commit c8c8cea Author: Eric Traut <eric@traut.com> Date: Sun Jan 28 21:14:11 2024 -0800 Fixed a bug that resulted in incorrect type evaluation when calling a `tuple` constructor with bidirectional type inference and the value passed to the constructor is an `Iterable[Any]`. This addresses microsoft#7085. (microsoft#7155) commit 7362545 Author: Eric Traut <eric@traut.com> Date: Sun Jan 28 18:07:14 2024 -0800 Added additional performance enhancements specifically for large unions of literal types. This addresses microsoft#7143. (microsoft#7154) commit 31f09f3 Author: Eric Traut <eric@traut.com> Date: Sun Jan 28 13:59:42 2024 -0800 Added missing check for the use of `Annotated` or a type alias defined with `Annotated` as the second argument to an `isinstance` or `issubclass` call. This produces an exception at runtime. This addresses microsoft#7092. commit 509bf69 Author: Eric Traut <eric@traut.com> Date: Sun Jan 28 13:49:04 2024 -0800 Fixed bug that resulted in incorrect type evaluation when a TypeAliasType is used in a value expression. This addresses microsoft#7109. commit 928d1cf Author: Eric Traut <eric@traut.com> Date: Sun Jan 28 13:37:41 2024 -0800 Fixed a bug that results in a false negative when the literal `typing.Any` is passed to a function that accepts a `type[T]`. `Any` is a special form and should not be compatible with `type`. This addresses microsoft#7082. (microsoft#7153) commit 1d4b1cc Author: Eric Traut <eric@traut.com> Date: Sun Jan 28 13:02:46 2024 -0800 Fixed bug in tuple type compatibility logic that resulted in a false negative when dest type includes an upacked unbounded tuple plus additional entries. This addresses microsoft#7115. (microsoft#7152) Fixed bug in tuple type compatibility logic that resulted in a false positive when dest type is `tuple[Any, ...]`. This addresses microsoft#7129. Improved error messages for tuple type mismatches that involve tuples with indeterminate types. commit 725f91f Author: Eric Traut <eric@traut.com> Date: Sun Jan 28 10:43:07 2024 -0800 Fixed a bug that results in a false positive when using a TypeVarTuple to capture the parameters of a generic callable that includes one or more default argument values. This addresses microsoft#7146. (microsoft#7150) commit e18963f Author: Eric Traut <eric@traut.com> Date: Sun Jan 28 10:23:40 2024 -0800 Added support for enum member aliases defined within an enum class. This addresses microsoft#7142. (microsoft#7149) commit c7d5fbd Author: Eric Traut <eric@traut.com> Date: Sun Jan 28 10:07:56 2024 -0800 Added special-case logic to handle `__name__` and a few other instanc… (microsoft#7148) * Added special-case logic to handle `__name__` and a few other instance variables defined in the `type` class so they are treated specially when accessed from a class instance. This addresses microsoft#7145. * Fixed style issue. commit 9fb26eb Author: Blake Naccarato <blake.naccarato@gmail.com> Date: Fri Jan 26 09:22:57 2024 -0800 Clarify virtual environment language in `exclude` part (microsoft#7133) commit ee4e30c Author: Eric Traut <eric@traut.com> Date: Thu Jan 25 20:04:18 2024 -0800 Added missing check for inappropriate use of `Final` in a value expression. This addresses microsoft#7094. commit 9c0c056 Author: Eric Traut <eric@traut.com> Date: Thu Jan 25 19:25:46 2024 -0800 Fixed regression that resulted in a false positive error when calling an abstract method on an abstract class that passes through the constraint solver (e.g. a generic decorator). This addresses microsoft#7105. (microsoft#7127) commit df5d48c Author: Eric Traut <eric@traut.com> Date: Thu Jan 25 01:26:05 2024 -0800 Fixed a bug that resulted in an incorrect type evaluation for a union type used as a runtime expression. The type should be `UnionType`, not `type[UnionType]`. (microsoft#7121) commit d94df3a Author: Eric Traut <eric@traut.com> Date: Thu Jan 25 00:44:20 2024 -0800 Changed handling of `tuple` with multiple unpacked embedded tuples. Type spec now clarifies this is OK as long as there are not multiple unbounded embedded tuples. This addresses microsoft#7114. (microsoft#7120) commit dcc33ca Author: Eric Traut <eric@traut.com> Date: Wed Jan 24 23:33:09 2024 -0800 Changed the way pyright translates `tuple[()]` into a specialized `Sequence`. It used to translate it to `Sequence[Any]`, but the typing spec now clarifies that it should be `Sequence[Never]`. This addresses microsoft#7118. (microsoft#7119) commit ebebb7f Author: Eric Traut <eric@traut.com> Date: Wed Jan 24 16:43:03 2024 -0800 Removed name consistency match for functional form of Enum. After a discussion in the typing community, it was decided that name consistency checks in some cases were unnecessary and inappropriate for type checkers. commit cfb1de0 Author: Eric Traut <eric@traut.com> Date: Mon Jan 22 12:29:32 2024 -0800 Fixed default for `reportImplicitStringConcatenation` in schema file. commit 5aa1044 Author: Eric Traut <eric@traut.com> Date: Sun Jan 21 20:35:11 2024 -0800 Added a few useful links to the docs. commit 3ebcef2 Author: Eric Traut <eric@traut.com> Date: Sun Jan 21 20:28:15 2024 -0800 Fixed minor doc bug. commit 015e143 Author: Eric Traut <eric@traut.com> Date: Sun Jan 21 17:05:31 2024 -0800 Removed tests from typeshed stubs. We don't use these, so they're just taking up unnecessary space. commit a168f42 Author: Eric Traut <eric@traut.com> Date: Sun Jan 21 09:50:43 2024 -0800 Published 1.1.348 commit 197ecd7 Author: Eric Traut <eric@traut.com> Date: Sun Jan 21 02:20:52 2024 -0800 Added three new diagnostic rules: `reportArgumentType` covers argument type compatibility checks, `reportAssignmentType` covers type compatibility checks for assignments, and `reportReturnType` covers type compatibility checks for return and yield statements. This partially addresses microsoft#6973. (microsoft#7077) commit 6ac1a7e Author: Eric Traut <eric@traut.com> Date: Sun Jan 21 01:47:43 2024 -0800 Added new diagnostic rule `reportCallIssue` that covers issues relating to call expressions and arguments. This partially addresses microsoft#6973. (microsoft#7076) commit 6363745 Author: Eric Traut <eric@traut.com> Date: Sun Jan 21 01:35:19 2024 -0800 Added two new diagnostic rules: `reportAttributeAccessIssue` is related to attribute accesses and `reportIndexIssue` is related to index operations and expressions. This partially addresses microsoft#6973. (microsoft#7075) commit 8270551 Author: Eric Traut <eric@traut.com> Date: Sun Jan 21 01:07:49 2024 -0800 Added two new diagnostic rules: `reportAbstractUsage` reports invalid use of abstract classes and methods and `reportOperatorIssue` covers diagnostics related to unary and binary operators. This partially addresses microsoft#6973. (microsoft#7074) commit ec6052e Author: Eric Traut <eric@traut.com> Date: Sun Jan 21 00:33:05 2024 -0800 Added two new diagnostic rules: `reportInvalidTypeArguments` reports invalid type arg usage and `reportRedeclaration` reports attempts to redeclare the type of a symbol. This partially addresses microsoft#6973. (microsoft#7073) commit 7a67f4f Author: Eric Traut <eric@traut.com> Date: Sun Jan 21 00:04:55 2024 -0800 Added two new diagnostic rules: `reportInconsistentOverload` reports inconsistencies between overload signatures and/or implementation and `reportNoOverloadImplementation` reports an overloaded function with a missing implementation. This partially addresses microsoft#6973. (microsoft#7072) commit 91960fb Author: Eric Traut <eric@traut.com> Date: Sat Jan 20 23:34:11 2024 -0800 Added new diagnostic rule `reportPossiblyUnboundVariable`, which is split off from `reportUnboundVariable`. This addresses microsoft#6896. (microsoft#7071) commit aa64fc5 Author: Eric Traut <eric@traut.com> Date: Sat Jan 20 22:21:42 2024 -0800 Added two new diagnostic rules: `reportAssertTypeFailure` for type mismatches detected by `typing.assert_type` and `reportUnusedExcept` for situations where an except statement is determined to be unreachable. (microsoft#7070) commit 04e0536 Author: Eric Traut <eric@traut.com> Date: Sat Jan 20 21:56:10 2024 -0800 Added new diagnostic rule `reportInvalidTypeForm` that controls reporting of invalid type expression forms. This partly addresses microsoft#6973. (microsoft#7069) commit 566f333 Author: Eric Traut <eric@traut.com> Date: Sat Jan 20 18:35:53 2024 -0800 Fixed a bug that led to a false negative when an illegal form of tuple was used: `tuple[*tuple[str], ...]`. (microsoft#7066) commit 3c36b30 Author: Eric Traut <eric@traut.com> Date: Sat Jan 20 17:32:49 2024 -0800 Changed diagnostic rule for the case where `Callable` is missing a second type argument. It should use `reportMissingTypeArgument`. commit 32f0685 Author: Eric Traut <eric@traut.com> Date: Sat Jan 20 17:32:16 2024 -0800 Modernized a few diagnostic messages to refer to `tuple` (lowercase) rather than `Tuple` (uppercase). commit 578ec79 Author: Eric Traut <eric@traut.com> Date: Sat Jan 20 14:55:25 2024 -0800 Fixed a bug that resulted in a false positive error and incorrect type evaluation when an assignment expression (walrus operator) is used in a comprehension. This addresses microsoft#6992. (microsoft#7064) commit 301ee7d Author: Eric Traut <eric@traut.com> Date: Sat Jan 20 14:36:29 2024 -0800 Disabled debug check that was accidentally enabled several builds ago. commit d21168e Author: Eric Traut <eric@traut.com> Date: Fri Jan 19 22:08:10 2024 -0800 Updated typeshed stubs to the latest version. commit af44054 Author: Eric Traut <eric@traut.com> Date: Fri Jan 19 21:55:50 2024 -0800 Fixed a bug that masked an error (false negative) under certain circumstances when evaluating a lambda. This addresses microsoft#7012. (microsoft#7059) commit 50f677c Author: Eric Traut <eric@traut.com> Date: Fri Jan 19 20:34:01 2024 -0800 Addressed a bug that led to a false positive (missing error) when a "bare" TypeVar is used as a base class in a class statement. This addresses microsoft#7023. (microsoft#7058) commit 335255f Author: Eric Traut <eric@traut.com> Date: Fri Jan 19 17:41:53 2024 -0800 Added error reporting for the situation where a generic instance vari… (microsoft#7057) * Added error reporting for the situation where a generic instance variable is accessed through a class object. This addresses microsoft#7051. * Fixed style issue. commit f10455e Author: Eric Traut <eric@traut.com> Date: Fri Jan 19 16:18:26 2024 -0800 Changed handling of `tuple[Any, ...]` so it is treated as though it's bidirectionally type compatible with all tuples regardless of length. This addresses microsoft#7053. (microsoft#7054) commit 9ec6beb Author: PylanceBot <99766470+PylanceBot@users.noreply.github.com> Date: Fri Jan 19 16:05:58 2024 -0800 pull-pylance-with-pyright- (microsoft#7052) Co-authored-by: GitHub Actions <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: rchiodo <rchiodo@microsoft.com> commit 0fc5682 Author: Eric Traut <eric@traut.com> Date: Fri Jan 19 09:40:37 2024 -0800 Improved handling of `Annotated` and other special forms when they are used in runtime value expressions rather than annotations. This addresses microsoft#7049. (microsoft#7050) commit 01b1aa0 Author: Eric Traut <eric@traut.com> Date: Fri Jan 19 00:33:55 2024 -0800 Added type enforcement for the `_value_` type in an Enum class. Also added enforcement for custom `__new__` and `__init__` method signatures. This addresses microsoft#7030 and microsoft#7029. (microsoft#7044) commit 85e8de6 Author: Eric Traut <eric@traut.com> Date: Thu Jan 18 20:26:01 2024 -0800 Added special-case logic for enum classes that are invoked as though they are being constructed. This addresses microsoft#7027. (microsoft#7042) commit 3f3a86a Author: Eric Traut <eric@traut.com> Date: Thu Jan 18 18:33:13 2024 -0800 Improved handling of custom Enum classes. This addresses microsoft#7024. (microsoft#7041) commit 4df6b6b Author: Eric Traut <eric@traut.com> Date: Thu Jan 18 17:07:50 2024 -0800 Fixed spec conformance issue with TypeVarTuple constraint solving. The spec indicates that if a TypeVarTuple is used multiple times in a callee's signature, the tuple must "match exactly". This addresses microsoft#6888. (microsoft#7040) commit 922e746 Author: Eric Traut <eric@traut.com> Date: Thu Jan 18 12:14:47 2024 -0800 Added check for name mismatch for enum classes defined using the functional syntax. This addresses microsoft#7025. (microsoft#7038) commit 81e85d1 Author: Rich Chiodo <rchiodo@users.noreply.github.com> Date: Thu Jan 18 10:54:40 2024 -0800 Fix combining paths with empty uri (microsoft#7037) * Fix combining paths with empty uri * Force empty to be case sensitive * Remove unneeded code
Pyright becomes very slow when it's dealing with huge literal types like this.
The gist includes a python file as well as a typescript file, since pyright is written in JS and typescript lsp is written in JS as well, I provided a typescript version as a proof that it can be much faster.
Running pyright on the python file or using pyright's autocompletion on
x: Icon = 'a
will take more than 20 seconds on M1 pro.Using tsserver's autocompletion on
const x: Icon = 'a
immediately lists all possible options.The text was updated successfully, but these errors were encountered: