-
Notifications
You must be signed in to change notification settings - Fork 768
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
code path returning None
falsely detected in graphviz Dot.subgraph
#4688
Comments
you could try adding a type stub ".pyi" file and specify two different function signatures based on or you could add a guard. import graphviz
dot = graphviz.Digraph(comment='Foo', format='png')
subgraph = dot.subgraph(name=f'cluster_Foo')
assert subgraph is not None, "Subgraph creation failed."
with subgraph as c:
# these all work fine:
c.attr(label='foo')
c.attr(style='filled', color='lightgrey')
c.node_attr.update(style='filled', color='white')
c.node('bar') |
The If this is a library that you rely upon, I recommend reaching out to its maintainers and requesting that they add proper static type annotations to it. Maintainers of many popular libraries have done this over the past few years. Another option is to create your own type stub that provides proper overload signatures for methods like this, but creating type stubs can be a lot of work. It also requires significant knowledge about the library you are stubbing. That's why it's best for the maintainers of a library to provide the type information as part of the library. If you find that type checking is producing too many false positive errors caused by untyped libraries, you can set You can also suppress all type checking diagnostics by setting In any case, pyright (and pylance) are working as intended here. I don't consider this a bug. |
@erictraut I assumed that pyright's heuristics allowed it to determine the return type based on inputs, in cases where an input's type determined the return type (like a simple dependent function). Is this not the case? @bschnurr Is it possible to add stubs for just that function, without impairing pyright's heuristics in other cases? |
The primary way pyright determines the return type of a function is based on the return statements in that function's implementation. In cases where this fails to produce a known return type, pyright can perform a more advanced technique called "call-site return type inference", but this approach is very expensive (because it requires reanalyzing the entire function at every call site) and is therefore used in very narrow situations. For more details, refer to this documentation. As I said above, pyright is working as intended here. You'll need to use one of the above workarounds if you want to eliminate the type error that you're seeing in this case. |
Is there an open feature request yet for adding more sophisticated/dependent return types? Something like Or (less powerfully) for inferring multiple type signatures depending on the inputs present, as @bschnurr suggested could be indicated with type stubs? |
We have a lot of experience with type inference. Pyright already implements return type inference techniques that are way more sophisticated than mypy or other Python type checkers — and even more sophisticated than TypeScript. More sophisticated return type inference (e.g. inferring overloads) wouldn't be a good solution. It would be very expensive (resulting in poor completion performance) and would work only in a small subset of cases. The solution here is for library authors to provide correct type information for their libraries. As I mentioned above, many libraries have started to do this over the past few years. If |
Environment data
Language Server version: 2023.8.10
OS and version: macOS 13.4.1
Python version (& distribution if applicable, e.g. Anaconda): 3.11.4
Code Snippet
Source of Dot.subgraph() method.
Expected behavior
Pylance recognizes that when
dot.subgraph()
is not passed agraph
keyword argument (i.e.,graph=None
), it always returns a contextmanager, neverNone
.Actual behavior
Pylance does not recognize this restriction.
Logs
N/A
The text was updated successfully, but these errors were encountered: