-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
Polynomial time-complexity of typing.overload #10004
Comments
This might be of some relevance: microsoft/pyright@d9f621e Seems Pyright only avoids quadratic complexity by skipping some form of consistency check when the number of overloads are large enough. I'm also interested in this feature, as I maintain one stubs package which currently uses about 300 overloads and another which would benefit from overloads but would require around 10 000 of them. |
Looks like a consistency check is probably the cause for Mypy as well: https://github.com/python/mypy/blob/master/mypy/checker.py#L486 |
#10922 can help here. This skips the quadratic check in files which mypy wouldn't have reported errors in (i.e. third party code), for instance, installed stub packages. |
Feature
The time complexity of dealing with function overloads in mypy appears to be polynomial. Naively, it seems this is not a fundamental limit (I have no experience in static analysis to base this on), below is a log-log graph of number of overloads vs execution time:
The data for the graph was generated with the following code:
And the plot:
The full dataset (
times.json
):I have inherited a library which has a very simple DSL to query data from a database and which returns a dictionary of different types depending on the input. Indeed, the language is so simple that I could map these to a collection of literal overloads, with a generic fallback for new/unmapped data. In practice this looks something like:
Being able to do static analysis (and completion) on such an interface would be hugely beneficial to its users for correctness and ease of use. Unfortunately the total number of overloads would need to be in the order of 100,000... clearly from the graph, this number is prohibitive with the current implementation of mypy's overload functionality.
Pitch
I'm therefore reaching out to find out if there is a known good reason for this time-complexity, and potentially seeking help/pointers to address the issue in the implementation.
Other places this could be useful
Truth be told, this is a highly specialised requirement which would apply to a fairly limited set of use cases (some of which are likely anti-patterns in the first place).
Given the high number of overloads involved for this time-complexity to become an issue (at around 100 overloads), it is clear that you'd almost always want to generate the stubs rather than hand-craft them. Examples of where it could come in handy are other string based lookups - I can imagine for example wanting to generate stubs for all of the JClasses that exist in JPype (which can be any Java classes available in the JVM), or perhaps for some interface that allows you to get hold of a CSS property by name and which returns a slightly different type for each of the ~520 valid tags. I could also imagine this being something that could be used for generating the permutations of a multiple-dispatch system.
The text was updated successfully, but these errors were encountered: