Skip to content

A sketchy hack to fix TypeGuard crash in logical expressions #11015

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

Merged
merged 5 commits into from
Aug 25, 2021

Conversation

ilevkivskyi
Copy link
Member

No description provided.

@ilevkivskyi ilevkivskyi requested a review from JukkaL August 24, 2021 17:58
@github-actions

This comment has been minimized.

Ivan Levkivskyi added 2 commits August 25, 2021 09:38
if not(f(x) or x):
return
g(x)
[builtins fixtures/tuple.pyi]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you also test is_x(a) or is_y(a) (two type guards) and type guard + isinstance (narrow down from e.g. List[object] to List[str] to test narrowing to a non-subtype with type guard in this context)?

@github-actions

This comment has been minimized.

@github-actions
Copy link
Contributor

Diff from mypy_primer, showing the effect of this PR on open source code:

sphinx (https://github.com/sphinx-doc/sphinx.git)
- sphinx/ext/coverage.py: note: In member "build_py_coverage" of class "CoverageBuilder":
- sphinx/ext/coverage.py:218:51: error: Item "object" of "Union[object, object]" has no attribute "__doc__"

starlette (https://github.com/encode/starlette.git)
- starlette/routing.py:84: error: <nothing> has no attribute "__name__"

arviz (https://github.com/arviz-devs/arviz.git)
- /tmp/mypy_primer/projects/_arviz_venv/lib/python3.8/site-packages/xarray/core/common.py:1659: error: INTERNAL ERROR -- Please try using mypy master on Github:
- https://mypy.readthedocs.io/en/stable/common_issues.html#using-a-development-mypy-build
- Please report a bug at https://github.com/python/mypy/issues
- version: 0.920+dev.7576f659d42ee271c78e69d7481b9d49517a49f6
+ arviz/plots/dotplot.py:226: error: Argument 1 to "hstack" has incompatible type "ndarray[Any, Any]"; expected "Sequence[Union[Sequence[Sequence[Sequence[Sequence[Sequence[Any]]]]], Union[Union[_SupportsArray[dtype[Any]], Sequence[_SupportsArray[dtype[Any]]], Sequence[Sequence[_SupportsArray[dtype[Any]]]], Sequence[Sequence[Sequence[_SupportsArray[dtype[Any]]]]], Sequence[Sequence[Sequence[Sequence[_SupportsArray[dtype[Any]]]]]]], Union[bool, int, float, complex, str, bytes, Sequence[Union[bool, int, float, complex, str, bytes]], Sequence[Sequence[Union[bool, int, float, complex, str, bytes]]], Sequence[Sequence[Sequence[Union[bool, int, float, complex, str, bytes]]]], Sequence[Sequence[Sequence[Sequence[Union[bool, int, float, complex, str, bytes]]]]]]]]]"
+ examples/matplotlib/mpl_plot_violin.py:14: error: Call to untyped function "load_arviz_data" in typed context
+ examples/matplotlib/mpl_plot_violin.py:15: error: Call to untyped function "plot_violin" in typed context
+ examples/matplotlib/mpl_plot_trace_vlines.py:14: error: Call to untyped function "load_arviz_data" in typed context
+ examples/matplotlib/mpl_plot_trace_circ.py:13: error: Call to untyped function "load_arviz_data" in typed context
+ examples/matplotlib/mpl_plot_trace_bars.py:14: error: Call to untyped function "load_arviz_data" in typed context
+ examples/matplotlib/mpl_plot_trace.py:14: error: Call to untyped function "load_arviz_data" in typed context
+ examples/matplotlib/mpl_plot_separation.py:13: error: Call to untyped function "load_arviz_data" in typed context
+ examples/matplotlib/mpl_plot_separation.py:15: error: Call to untyped function "plot_separation" in typed context
+ examples/matplotlib/mpl_plot_rank.py:14: error: Call to untyped function "load_arviz_data" in typed context
+ examples/matplotlib/mpl_plot_rank.py:15: error: Call to untyped function "plot_rank" in typed context
+ examples/matplotlib/mpl_plot_ppc_cumulative.py:14: error: Call to untyped function "load_arviz_data" in typed context
+ examples/matplotlib/mpl_plot_ppc_cumulative.py:15: error: Call to untyped function "plot_ppc" in typed context
+ examples/matplotlib/mpl_plot_ppc.py:14: error: Call to untyped function "load_arviz_data" in typed context
+ examples/matplotlib/mpl_plot_ppc.py:15: error: Call to untyped function "plot_ppc" in typed context
+ examples/matplotlib/mpl_plot_posterior.py:14: error: Call to untyped function "load_arviz_data" in typed context
+ examples/matplotlib/mpl_plot_posterior.py:17: error: Call to untyped function "plot_posterior" in typed context
+ examples/matplotlib/mpl_plot_parallel_rank.py:13: error: Call to untyped function "load_arviz_data" in typed context
+ examples/matplotlib/mpl_plot_parallel_rank.py:14: error: Call to untyped function "plot_parallel" in typed context
+ examples/matplotlib/mpl_plot_parallel_normal.py:14: error: Call to untyped function "load_arviz_data" in typed context
+ examples/matplotlib/mpl_plot_parallel_normal.py:15: error: Call to untyped function "plot_parallel" in typed context
+ examples/matplotlib/mpl_plot_parallel_minmax.py:14: error: Call to untyped function "load_arviz_data" in typed context
+ examples/matplotlib/mpl_plot_parallel_minmax.py:15: error: Call to untyped function "plot_parallel" in typed context
+ examples/matplotlib/mpl_plot_parallel.py:14: error: Call to untyped function "load_arviz_data" in typed context
+ examples/matplotlib/mpl_plot_parallel.py:15: error: Call to untyped function "plot_parallel" in typed context
+ examples/matplotlib/mpl_plot_pair_point_estimate.py:12: error: Call to untyped function "load_arviz_data" in typed context
+ examples/matplotlib/mpl_plot_pair_kde_hdi.py:14: error: Call to untyped function "load_arviz_data" in typed context
+ examples/matplotlib/mpl_plot_pair_kde.py:14: error: Call to untyped function "load_arviz_data" in typed context
+ examples/matplotlib/mpl_plot_pair_hex.py:14: error: Call to untyped function "load_arviz_data" in typed context
+ examples/matplotlib/mpl_plot_pair.py:14: error: Call to untyped function "load_arviz_data" in typed context
+ examples/matplotlib/mpl_plot_mcse_errorbar.py:14: error: Call to untyped function "load_arviz_data" in typed context
+ examples/matplotlib/mpl_plot_mcse_errorbar.py:15: error: Call to untyped function "plot_mcse" in typed context
+ examples/matplotlib/mpl_plot_mcse.py:14: error: Call to untyped function "load_arviz_data" in typed context
+ examples/matplotlib/mpl_plot_mcse.py:15: error: Call to untyped function "plot_mcse" in typed context
+ examples/matplotlib/mpl_plot_loo_pit_overlay.py:14: error: Call to untyped function "load_arviz_data" in typed context
+ examples/matplotlib/mpl_plot_loo_pit_overlay.py:16: error: Call to untyped function "plot_loo_pit" in typed context
+ examples/matplotlib/mpl_plot_loo_pit_ecdf.py:14: error: Call to untyped function "load_arviz_data" in typed context
+ examples/matplotlib/mpl_plot_loo_pit_ecdf.py:16: error: Call to untyped function "plot_loo_pit" in typed context
+ examples/matplotlib/mpl_plot_lm.py:15: error: Call to untyped function "load_arviz_data" in typed context
+ examples/matplotlib/mpl_plot_lm.py:22: error: Call to untyped function "plot_lm" in typed context
+ examples/matplotlib/mpl_plot_khat.py:14: error: Call to untyped function "load_arviz_data" in typed context
+ examples/matplotlib/mpl_plot_khat.py:15: error: Call to untyped function "loo" in typed context
+ examples/matplotlib/mpl_plot_khat.py:17: error: Call to untyped function "plot_khat" in typed context
+ examples/matplotlib/mpl_plot_kde_quantiles.py:16: error: Call to untyped function "plot_kde" in typed context
+ examples/matplotlib/mpl_plot_kde_2d_hdi.py:18: error: Call to untyped function "plot_kde" in typed context
+ examples/matplotlib/mpl_plot_kde_2d_bis.py:14: error: Call to untyped function "plot_kde" in typed context
+ examples/matplotlib/mpl_plot_kde_2d.py:15: error: Call to untyped function "plot_kde" in typed context
+ examples/matplotlib/mpl_plot_kde.py:15: error: Call to untyped function "load_arviz_data" in typed context
+ examples/matplotlib/mpl_plot_kde.py:18: error: Call to untyped function "concatenate" in typed context
+ examples/matplotlib/mpl_plot_kde.py:20: error: Call to untyped function "plot_kde" in typed context
+ examples/matplotlib/mpl_plot_joint.py:15: error: Call to untyped function "load_arviz_data" in typed context
+ examples/matplotlib/mpl_plot_hdi.py:17: error: Call to untyped function "plot_hdi" in typed context
+ examples/matplotlib/mpl_plot_forest_ridge.py:14: error: Call to untyped function "load_arviz_data" in typed context
+ examples/matplotlib/mpl_plot_forest_ridge.py:15: error: Call to untyped function "plot_forest" in typed context
+ examples/matplotlib/mpl_plot_forest.py:14: error: Call to untyped function "load_arviz_data" in typed context
+ examples/matplotlib/mpl_plot_forest.py:15: error: Call to untyped function "load_arviz_data" in typed context
+ examples/matplotlib/mpl_plot_forest.py:16: error: Call to untyped function "plot_forest" in typed context
+ examples/matplotlib/mpl_plot_ess_quantile.py:14: error: Call to untyped function "load_arviz_data" in typed context
+ examples/matplotlib/mpl_plot_ess_quantile.py:16: error: Call to untyped function "plot_ess" in typed context
+ examples/matplotlib/mpl_plot_ess_local.py:14: error: Call to untyped function "load_arviz_data" in typed context
+ examples/matplotlib/mpl_plot_ess_local.py:16: error: Call to untyped function "plot_ess" in typed context
+ examples/matplotlib/mpl_plot_ess_evolution.py:14: error: Call to untyped function "load_arviz_data" in typed context
+ examples/matplotlib/mpl_plot_ess_evolution.py:16: error: Call to untyped function "plot_ess" in typed context
+ examples/matplotlib/mpl_plot_energy.py:14: error: Call to untyped function "load_arviz_data" in typed context
+ examples/matplotlib/mpl_plot_energy.py:15: error: Call to untyped function "plot_energy" in typed context
+ examples/matplotlib/mpl_plot_elpd.py:13: error: Call to untyped function "load_arviz_data" in typed context
+ examples/matplotlib/mpl_plot_elpd.py:14: error: Call to untyped function "load_arviz_data" in typed context
+ examples/matplotlib/mpl_plot_elpd.py:16: error: Call to untyped function "plot_elpd" in typed context
+ examples/matplotlib/mpl_plot_dot.py:16: error: Call to untyped function "plot_dot" in typed context
+ examples/matplotlib/mpl_plot_dist.py:19: error: Call to untyped function "plot_dist" in typed context
+ examples/matplotlib/mpl_plot_dist.py:20: error: Call to untyped function "plot_dist" in typed context
+ examples/matplotlib/mpl_plot_density.py:14: error: Call to untyped function "load_arviz_data" in typed context
+ examples/matplotlib/mpl_plot_density.py:15: error: Call to untyped function "load_arviz_data" in typed context
+ examples/matplotlib/mpl_plot_density.py:16: error: Call to untyped function "plot_density" in typed context
+ examples/matplotlib/mpl_plot_compare.py:17: error: Call to untyped function "load_arviz_data" in typed context
+ examples/matplotlib/mpl_plot_compare.py:18: error: Call to untyped function "load_arviz_data" in typed context
+ examples/matplotlib/mpl_plot_compare.py:21: error: Call to untyped function "plot_compare" in typed context
+ examples/matplotlib/mpl_plot_bpv_tstat.py:13: error: Call to untyped function "load_arviz_data" in typed context
+ examples/matplotlib/mpl_plot_bpv_tstat.py:14: error: Call to untyped function "plot_bpv" in typed context
+ examples/matplotlib/mpl_plot_bpv.py:13: error: Call to untyped function "load_arviz_data" in typed context
+ examples/matplotlib/mpl_plot_bpv.py:14: error: Call to untyped function "plot_bpv" in typed context
+ examples/matplotlib/mpl_plot_autocorr.py:14: error: Call to untyped function "load_arviz_data" in typed context
+ examples/matplotlib/mpl_plot_autocorr.py:15: error: Call to untyped function "plot_autocorr" in typed context
+ examples/bokeh/bokeh_plot_violin.py:9: error: Call to untyped function "load_arviz_data" in typed context
+ examples/bokeh/bokeh_plot_violin.py:10: error: Call to untyped function "plot_violin" in typed context
+ examples/bokeh/bokeh_plot_trace_vlines.py:9: error: Call to untyped function "load_arviz_data" in typed context
+ examples/bokeh/bokeh_plot_trace_bars.py:9: error: Call to untyped function "load_arviz_data" in typed context
+ examples/bokeh/bokeh_plot_trace.py:9: error: Call to untyped function "load_arviz_data" in typed context
+ examples/bokeh/bokeh_plot_separation.py:9: error: Call to untyped function "load_arviz_data" in typed context
+ examples/bokeh/bokeh_plot_separation.py:11: error: Call to untyped function "plot_separation" in typed context
+ examples/bokeh/bokeh_plot_rank.py:9: error: Call to untyped function "load_arviz_data" in typed context
+ examples/bokeh/bokeh_plot_rank.py:10: error: Call to untyped function "plot_rank" in typed context
+ examples/bokeh/bokeh_plot_ppc_cumulative.py:9: error: Call to untyped function "load_arviz_data" in typed context
+ examples/bokeh/bokeh_plot_ppc_cumulative.py:10: error: Call to untyped function "plot_ppc" in typed context
+ examples/bokeh/bokeh_plot_ppc.py:9: error: Call to untyped function "load_arviz_data" in typed context
+ examples/bokeh/bokeh_plot_ppc.py:10: error: Call to untyped function "plot_ppc" in typed context
+ examples/bokeh/bokeh_plot_posterior.py:9: error: Call to untyped function "load_arviz_data" in typed context
+ examples/bokeh/bokeh_plot_posterior.py:12: error: Call to untyped function "plot_posterior" in typed context
+ examples/bokeh/bokeh_plot_parallel_rank.py:9: error: Call to untyped function "load_arviz_data" in typed context
+ examples/bokeh/bokeh_plot_parallel_rank.py:10: error: Call to untyped function "plot_parallel" in typed context
+ examples/bokeh/bokeh_plot_parallel_normal.py:9: error: Call to untyped function "load_arviz_data" in typed context
+ examples/bokeh/bokeh_plot_parallel_normal.py:10: error: Call to untyped function "plot_parallel" in typed context
+ examples/bokeh/bokeh_plot_parallel_minmax.py:9: error: Call to untyped function "load_arviz_data" in typed context
+ examples/bokeh/bokeh_plot_parallel_minmax.py:10: error: Call to untyped function "plot_parallel" in typed context
+ examples/bokeh/bokeh_plot_parallel.py:9: error: Call to untyped function "load_arviz_data" in typed context
+ examples/bokeh/bokeh_plot_parallel.py:10: error: Call to untyped function "plot_parallel" in typed context
+ examples/bokeh/bokeh_plot_pair_point_estimate.py:11: error: Call to untyped function "load_arviz_data" in typed context
+ examples/bokeh/bokeh_plot_pair_kde_hdi.py:12: error: Call to untyped function "load_arviz_data" in typed context
+ examples/bokeh/bokeh_plot_pair_kde.py:9: error: Call to untyped function "load_arviz_data" in typed context
+ examples/bokeh/bokeh_plot_pair_hex.py:9: error: Call to untyped function "load_arviz_data" in typed context
+ examples/bokeh/bokeh_plot_pair.py:9: error: Call to untyped function "load_arviz_data" in typed context
+ examples/bokeh/bokeh_plot_mcse_errorbar.py:9: error: Call to untyped function "load_arviz_data" in typed context
+ examples/bokeh/bokeh_plot_mcse_errorbar.py:10: error: Call to untyped function "plot_mcse" in typed context
+ examples/bokeh/bokeh_plot_mcse.py:9: error: Call to untyped function "load_arviz_data" in typed context
+ examples/bokeh/bokeh_plot_mcse.py:10: error: Call to untyped function "plot_mcse" in typed context
+ examples/bokeh/bokeh_plot_loo_pit_overlay.py:10: error: Call to untyped function "load_arviz_data" in typed context
+ examples/bokeh/bokeh_plot_loo_pit_overlay.py:12: error: Call to untyped function "plot_loo_pit" in typed context
+ examples/bokeh/bokeh_plot_loo_pit_ecdf.py:10: error: Call to untyped function "load_arviz_data" in typed context
+ examples/bokeh/bokeh_plot_loo_pit_ecdf.py:12: error: Call to untyped function "plot_loo_pit" in typed context
+ examples/bokeh/bokeh_plot_lm.py:11: error: Call to untyped function "load_arviz_data" in typed context
+ examples/bokeh/bokeh_plot_lm.py:19: error: Call to untyped function "plot_lm" in typed context
+ examples/bokeh/bokeh_plot_khat.py:9: error: Call to untyped function "load_arviz_data" in typed context
+ examples/bokeh/bokeh_plot_khat.py:10: error: Call to untyped function "loo" in typed context
+ examples/bokeh/bokeh_plot_khat.py:12: error: Call to untyped function "plot_khat" in typed context
+ examples/bokeh/bokeh_plot_kde_quantiles.py:12: error: Call to untyped function "plot_kde" in typed context
+ examples/bokeh/bokeh_plot_kde_2d_hdi.py:17: error: Call to untyped function "plot_kde" in typed context
+ examples/bokeh/bokeh_plot_kde_2d_bis.py:13: error: Call to untyped function "plot_kde" in typed context
+ examples/bokeh/bokeh_plot_kde_2d.py:11: error: Call to untyped function "plot_kde" in typed context
+ examples/bokeh/bokeh_plot_kde.py:12: error: Call to untyped function "load_arviz_data" in typed context
+ examples/bokeh/bokeh_plot_kde.py:15: error: Call to untyped function "concatenate" in typed context
+ examples/bokeh/bokeh_plot_kde.py:20: error: Call to untyped function "plot_kde" in typed context
+ examples/bokeh/bokeh_plot_joint.py:9: error: Call to untyped function "load_arviz_data" in typed context
+ examples/bokeh/bokeh_plot_hdi.py:17: error: Call to untyped function "plot_hdi" in typed context
+ examples/bokeh/bokeh_plot_forest_ridge.py:9: error: Call to untyped function "load_arviz_data" in typed context
+ examples/bokeh/bokeh_plot_forest_ridge.py:10: error: Call to untyped function "plot_forest" in typed context
+ examples/bokeh/bokeh_plot_forest.py:9: error: Call to untyped function "load_arviz_data" in typed context
+ examples/bokeh/bokeh_plot_forest.py:10: error: Call to untyped function "load_arviz_data" in typed context
+ examples/bokeh/bokeh_plot_forest.py:11: error: Call to untyped function "plot_forest" in typed context
+ examples/bokeh/bokeh_plot_ess_quantile.py:9: error: Call to untyped function "load_arviz_data" in typed context
+ examples/bokeh/bokeh_plot_ess_quantile.py:11: error: Call to untyped function "plot_ess" in typed context
+ examples/bokeh/bokeh_plot_ess_local.py:9: error: Call to untyped function "load_arviz_data" in typed context
+ examples/bokeh/bokeh_plot_ess_local.py:11: error: Call to untyped function "plot_ess" in typed context
+ examples/bokeh/bokeh_plot_ess_evolution.py:9: error: Call to untyped function "load_arviz_data" in typed context
+ examples/bokeh/bokeh_plot_ess_evolution.py:11: error: Call to untyped function "plot_ess" in typed context
+ examples/bokeh/bokeh_plot_energy.py:9: error: Call to untyped function "load_arviz_data" in typed context
+ examples/bokeh/bokeh_plot_energy.py:10: error: Call to untyped function "plot_energy" in typed context
+ examples/bokeh/bokeh_plot_elpd.py:9: error: Call to untyped function "load_arviz_data" in typed context
+ examples/bokeh/bokeh_plot_elpd.py:10: error: Call to untyped function "load_arviz_data" in typed context
+ examples/bokeh/bokeh_plot_elpd.py:12: error: Call to untyped function "plot_elpd" in typed context
+ examples/bokeh/bokeh_plot_dot.py:12: error: Call to untyped function "plot_dot" in typed context
+ examples/bokeh/bokeh_plot_dist.py:20: error: Call to untyped function "plot_dist" in typed context
+ examples/bokeh/bokeh_plot_dist.py:21: error: Call to untyped function "plot_dist" in typed context
+ examples/bokeh/bokeh_plot_density.py:9: error: Call to untyped function "load_arviz_data" in typed context
+ examples/bokeh/bokeh_plot_density.py:10: error: Call to untyped function "load_arviz_data" in typed context
+ examples/bokeh/bokeh_plot_density.py:11: error: Call to untyped function "plot_density" in typed context
+ examples/bokeh/bokeh_plot_compare.py:11: error: Call to untyped function "load_arviz_data" in typed context
+ examples/bokeh/bokeh_plot_compare.py:12: error: Call to untyped function "load_arviz_data" in typed context
+ examples/bokeh/bokeh_plot_compare.py:15: error: Call to untyped function "plot_compare" in typed context
+ examples/bokeh/bokeh_plot_bpv_tstat.py:9: note: (Skipping most remaining errors due to unresolved imports or missing stubs; fix these first)
- Traceback (most recent call last):
-   File "/tmp/mypy_primer/old_mypy/venv/bin/mypy", line 8, in <module>
-     sys.exit(console_entry())
-   File "/tmp/mypy_primer/old_mypy/venv/lib/python3.8/site-packages/mypy/__main__.py", line 11, in console_entry
-     main(None, sys.stdout, sys.stderr)
-   File "/tmp/mypy_primer/old_mypy/venv/lib/python3.8/site-packages/mypy/main.py", line 87, in main
-     res, messages, blockers = run_build(sources, options, fscache, t0, stdout, stderr)
-   File "/tmp/mypy_primer/old_mypy/venv/lib/python3.8/site-packages/mypy/main.py", line 165, in run_build
-     res = build.build(sources, options, None, flush_errors, fscache, stdout, stderr)
-   File "/tmp/mypy_primer/old_mypy/venv/lib/python3.8/site-packages/mypy/build.py", line 179, in build
-     result = _build(
-   File "/tmp/mypy_primer/old_mypy/venv/lib/python3.8/site-packages/mypy/build.py", line 254, in _build
-     graph = dispatch(sources, manager, stdout)
-   File "/tmp/mypy_primer/old_mypy/venv/lib/python3.8/site-packages/mypy/build.py", line 2707, in dispatch
-     process_graph(graph, manager)
-   File "/tmp/mypy_primer/old_mypy/venv/lib/python3.8/site-packages/mypy/build.py", line 3031, in process_graph
-     process_stale_scc(graph, scc, manager)
-   File "/tmp/mypy_primer/old_mypy/venv/lib/python3.8/site-packages/mypy/build.py", line 3129, in process_stale_scc
-     graph[id].type_check_first_pass()
-   File "/tmp/mypy_primer/old_mypy/venv/lib/python3.8/site-packages/mypy/build.py", line 2175, in type_check_first_pass
-     self.type_checker().check_first_pass()
-   File "/tmp/mypy_primer/old_mypy/venv/lib/python3.8/site-packages/mypy/checker.py", line 295, in check_first_pass
-     self.accept(d)
-   File "/tmp/mypy_primer/old_mypy/venv/lib/python3.8/site-packages/mypy/checker.py", line 402, in accept
-     stmt.accept(self)
-   File "/tmp/mypy_primer/old_mypy/venv/lib/python3.8/site-packages/mypy/nodes.py", line 530, in accept
-     return visitor.visit_overloaded_func_def(self)
-   File "/tmp/mypy_primer/old_mypy/venv/lib/python3.8/site-packages/mypy/checker.py", line 435, in visit_overloaded_func_def
-     self._visit_overloaded_func_def(defn)
-   File "/tmp/mypy_primer/old_mypy/venv/lib/python3.8/site-packages/mypy/checker.py", line 457, in _visit_overloaded_func_def
-     defn.impl.accept(self)
-   File "/tmp/mypy_primer/old_mypy/venv/lib/python3.8/site-packages/mypy/nodes.py", line 696, in accept
-     return visitor.visit_func_def(self)
-   File "/tmp/mypy_primer/old_mypy/venv/lib/python3.8/site-packages/mypy/checker.py", line 734, in visit_func_def
-     self._visit_func_def(defn)
-   File "/tmp/mypy_primer/old_mypy/venv/lib/python3.8/site-packages/mypy/checker.py", line 738, in _visit_func_def
-     self.check_func_item(defn, name=defn.name)
-   File "/tmp/mypy_primer/old_mypy/venv/lib/python3.8/site-packages/mypy/checker.py", line 800, in check_func_item
-     self.check_func_def(defn, typ, name)
-   File "/tmp/mypy_primer/old_mypy/venv/lib/python3.8/site-packages/mypy/checker.py", line 983, in check_func_def
-     self.accept(item.body)
-   File "/tmp/mypy_primer/old_mypy/venv/lib/python3.8/site-packages/mypy/checker.py", line 402, in accept
-     stmt.accept(self)
-   File "/tmp/mypy_primer/old_mypy/venv/lib/python3.8/site-packages/mypy/nodes.py", line 1024, in accept
-     return visitor.visit_block(self)
-   File "/tmp/mypy_primer/old_mypy/venv/lib/python3.8/site-packages/mypy/checker.py", line 1996, in visit_block
-     self.accept(s)
-   File "/tmp/mypy_primer/old_mypy/venv/lib/python3.8/site-packages/mypy/checker.py", line 402, in accept
-     stmt.accept(self)
-   File "/tmp/mypy_primer/old_mypy/venv/lib/python3.8/site-packages/mypy/nodes.py", line 1216, in accept
-     return visitor.visit_if_stmt(self)
-   File "/tmp/mypy_primer/old_mypy/venv/lib/python3.8/site-packages/mypy/checker.py", line 3313, in visit_if_stmt
-     self.accept(b)
-   File "/tmp/mypy_primer/old_mypy/venv/lib/python3.8/site-packages/mypy/checker.py", line 402, in accept
-     stmt.accept(self)
-   File "/tmp/mypy_primer/old_mypy/venv/lib/python3.8/site-packages/mypy/nodes.py", line 1024, in accept
-     return visitor.visit_block(self)
-   File "/tmp/mypy_primer/old_mypy/venv/lib/python3.8/site-packages/mypy/checker.py", line 1996, in visit_block
-     self.accept(s)
-   File "/tmp/mypy_primer/old_mypy/venv/lib/python3.8/site-packages/mypy/checker.py", line 402, in accept
-     stmt.accept(self)
-   File "/tmp/mypy_primer/old_mypy/venv/lib/python3.8/site-packages/mypy/nodes.py", line 1216, in accept
-     return visitor.visit_if_stmt(self)
-   File "/tmp/mypy_primer/old_mypy/venv/lib/python3.8/site-packages/mypy/checker.py", line 3303, in visit_if_stmt
-     t = get_proper_type(self.expr_checker.accept(e))
-   File "/tmp/mypy_primer/old_mypy/venv/lib/python3.8/site-packages/mypy/checkexpr.py", line 3910, in accept
-     typ = node.accept(self)
-   File "/tmp/mypy_primer/old_mypy/venv/lib/python3.8/site-packages/mypy/nodes.py", line 1663, in accept
-     return visitor.visit_unary_expr(self)
-   File "/tmp/mypy_primer/old_mypy/venv/lib/python3.8/site-packages/mypy/checkexpr.py", line 2873, in visit_unary_expr
-     operand_type = self.accept(e.expr)
-   File "/tmp/mypy_primer/old_mypy/venv/lib/python3.8/site-packages/mypy/checkexpr.py", line 3910, in accept
-     typ = node.accept(self)
-   File "/tmp/mypy_primer/old_mypy/venv/lib/python3.8/site-packages/mypy/nodes.py", line 1600, in accept
-     return visitor.visit_call_expr(self)
-   File "/tmp/mypy_primer/old_mypy/venv/lib/python3.8/site-packages/mypy/checkexpr.py", line 277, in visit_call_expr
-     return self.visit_call_expr_inner(e, allow_none_return=allow_none_return)
-   File "/tmp/mypy_primer/old_mypy/venv/lib/python3.8/site-packages/mypy/checkexpr.py", line 366, in visit_call_expr_inner
-     ret_type = self.check_call_expr_with_callee_type(callee_type, e, fullname,
-   File "/tmp/mypy_primer/old_mypy/venv/lib/python3.8/site-packages/mypy/checkexpr.py", line 873, in check_call_expr_with_callee_type
-     return self.check_call(callee_type, e.args, e.arg_kinds, e,
-   File "/tmp/mypy_primer/old_mypy/venv/lib/python3.8/site-packages/mypy/checkexpr.py", line 933, in check_call
-     return self.check_callable_call(callee, args, arg_kinds, context, arg_names,
-   File "/tmp/mypy_primer/old_mypy/venv/lib/python3.8/site-packages/mypy/checkexpr.py", line 1030, in check_callable_call
-     self.check_argument_types(arg_types, arg_kinds, args, callee, formal_to_actual, context,
-   File "/tmp/mypy_primer/old_mypy/venv/lib/python3.8/site-packages/mypy/checkexpr.py", line 1493, in check_argument_types
-     check_arg(expanded_actual, actual_type, arg_kinds[actual],
-   File "/tmp/mypy_primer/old_mypy/venv/lib/python3.8/site-packages/mypy/checkexpr.py", line 1523, in check_arg
-     elif not is_subtype(caller_type, callee_type):
-   File "/tmp/mypy_primer/old_mypy/venv/lib/python3.8/site-packages/mypy/subtypes.py", line 93, in is_subtype
-     return _is_subtype(left, right,
-   File "/tmp/mypy_primer/old_mypy/venv/lib/python3.8/site-packages/mypy/subtypes.py", line 147, in _is_subtype
-     return left.accept(SubtypeVisitor(orig_right,
-   File "/tmp/mypy_primer/old_mypy/venv/lib/python3.8/site-packages/mypy/types.py", line 1747, in accept
-     return visitor.visit_union_type(self)
-   File "/tmp/mypy_primer/old_mypy/venv/lib/python3.8/site-packages/mypy/subtypes.py", line 476, in visit_union_type
-     return all(self._is_subtype(item, self.orig_right) for item in left.items)
-   File "/tmp/mypy_primer/old_mypy/venv/lib/python3.8/site-packages/mypy/subtypes.py", line 476, in <genexpr>
-     return all(self._is_subtype(item, self.orig_right) for item in left.items)
-   File "/tmp/mypy_primer/old_mypy/venv/lib/python3.8/site-packages/mypy/subtypes.py", line 205, in _is_subtype
-     return is_subtype(left, right,
-   File "/tmp/mypy_primer/old_mypy/venv/lib/python3.8/site-packages/mypy/subtypes.py", line 93, in is_subtype
-     return _is_subtype(left, right,
-   File "/tmp/mypy_primer/old_mypy/venv/lib/python3.8/site-packages/mypy/subtypes.py", line 147, in _is_subtype
-     return left.accept(SubtypeVisitor(orig_right,
-   File "/tmp/mypy_primer/old_mypy/venv/lib/python3.8/site-packages/mypy/types.py", line 296, in accept
-     return visitor.visit_type_guard_type(self)
-   File "/tmp/mypy_primer/old_mypy/venv/lib/python3.8/site-packages/mypy/subtypes.py", line 479, in visit_type_guard_type
-     raise RuntimeError("TypeGuard should not appear here")
- RuntimeError: TypeGuard should not appear here

tornado (https://github.com/tornadoweb/tornado.git)
+ tornado/testing.py:587: error: Incompatible types in assignment (expression has type "Union[Generator[Any, Any, Any], CoroutineType]", variable has type "Union[Generator[Any, Any, Any], Coroutine[Any, Any, Any], None]")
+ tornado/testing.py:590: error: Incompatible return value type (got "Union[Generator[Any, Any, Any], CoroutineType, Coroutine[Any, Any, Any]]", expected "Union[Generator[Any, Any, Any], Coroutine[Any, Any, Any]]")

Copy link
Collaborator

@JukkaL JukkaL left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR! Looks reasonable -- this is still a bit dangerous, but it's much better already.

@JukkaL
Copy link
Collaborator

JukkaL commented Aug 25, 2021

This also seems to fix bunch of crashes in code bases checked with mypy primer -- excellent!

@JukkaL JukkaL merged commit 2a1cea4 into python:master Aug 25, 2021
@ilevkivskyi ilevkivskyi deleted the fix-typeguard branch August 25, 2021 11:32
hauntsaninja added a commit that referenced this pull request Sep 14, 2021
Fixes #11007, fixes #10899, fixes #10647

Since the initial implementation of TypeGuard, there have been several fixes quickly applied to make mypy not crash on various TypeGuard things. This includes #10496, #10683 and #11015. We'll discuss how this PR relates to each of these three changes.

In particular, #10496 seems incorrect. As A5rocks discusses in #10899 , it introduces confusion between a type guarded variable and a TypeGuard[T]. This PR basically walks back that change entirely and renames TypeGuardType to TypeGuardedType to reduce that possible confusion.

Now, we still have the issue that TypeGuardedTypes are getting everywhere and causing unhappiness. I see two high level solutions to this:
a) Make TypeGuardedType a proper type, then delegate to the wrapped type in a bunch of type visitors and arbitrary amounts of other places where multiple types interact, and hope that we got all of them,
b) Make TypeGuardedType as an improper type (as it was in the original implementation)! Similar to TypeAliasType, it's just a wrapper for another type, so we unwrap it in get_proper_type. This is the approach this PR takes. This might feel controversial, but I think it could be the better option. It also means that if we type check we won't get type guard crashes.

#10683 is basically "remove call that leads to crash from the stacktrace". I think the join here (that ends up being with the wrapped type of the TypeGuardedType) is actually fine: if it's different, it tells us that the type changed, which is what we want to know. So seems fine to remove the special casing.

Finally, #11015. This is the other contentious part of this PR. I liked the idea of moving the core "type guard overrides narrowing" idea into meet.py, so I kept that. But my changes ended up regressing a reveal_type testTypeGuardNestedRestrictionAny test that was added. But it's not really clear to me how that worked or really, what it tested. I tried writing a simpler version of what I thought the test was meant to test (this is testTypeGuardMultipleCondition added in this PR), but that fails on master.

Anyway, this should at least fix the type guard crashes that have been coming up.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants