typeshed icon indicating copy to clipboard operation
typeshed copied to clipboard

Make asyncio.Task covariant

Open Dreamsorcerer opened this issue 2 years ago • 11 comments

https://github.com/python/mypy/issues/13689#issuecomment-1253995236

Dreamsorcerer avatar Sep 21 '22 18:09 Dreamsorcerer

See my comment on the mypy issue, I think making Future covariant is unsound

hauntsaninja avatar Sep 21 '22 18:09 hauntsaninja

Will it still work by making Task covariant and Future not?

Dreamsorcerer avatar Sep 21 '22 18:09 Dreamsorcerer

Thanks @hauntsaninja, I missed that method. We can't make Task covariant either, because Task.set_result would still be unsafe.

JelleZijlstra avatar Sep 21 '22 18:09 JelleZijlstra

Task.set_result always raises, so I think you could get away with that.

hauntsaninja avatar Sep 21 '22 18:09 hauntsaninja

Indeed, docs says:

Task inherits from Future all of its APIs except Future.set_result() and Future.set_exception().

Dreamsorcerer avatar Sep 21 '22 18:09 Dreamsorcerer

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

anyio (https://github.com/agronholm/anyio)
+ src/anyio/_backends/_asyncio.py:807: error: No return value expected  [return-value]

websockets (https://github.com/aaugustin/websockets)
+ src/websockets/__main__.py:129: error: Need type annotation for "message"

python-chess (https://github.com/niklasf/python-chess)
+ chess/engine.py:2833: error: Need type annotation for "returncode"

tornado (https://github.com/tornadoweb/tornado)
+ tornado/gen.py:527: error: Need type annotation for "result_list" (hint: "result_list: List[<type>] = ...")
+ tornado/gen.py:769: error: Need type annotation for "value"
+ tornado/tcpclient.py:138: error: Need type annotation for "stream"
+ tornado/tcpclient.py:261: error: Need type annotation for "addrinfo"
+ tornado/tcpclient.py:275: error: <nothing> object is not iterable
+ tornado/tcpclient.py:281: error: Need type annotation for "stream"
+ tornado/tcpclient.py:283: error: Cannot determine type of "stream"
+ tornado/auth.py:143: error: Need type annotation for "resp"
+ tornado/auth.py:329: error: Need type annotation for "response"
+ tornado/auth.py:377: error: Need type annotation for "response"
+ tornado/auth.py:659: error: Need type annotation for "response"
+ tornado/auth.py:734: error: Need type annotation for "response"
+ tornado/auth.py:807: error: Need type annotation for "response"
+ tornado/auth.py:940: error: Need type annotation for "response"
+ tornado/test/asyncio_test.py:62: error: Need type annotation for "x"
+ tornado/test/gen_test.py:384: error: Need type annotation for "result"

jax (https://github.com/google/jax)
+ jax/experimental/gda_serialization/serialization.py:54: error: Need type annotation for "local_arrays"  [var-annotated]
+ jax/experimental/gda_serialization/serialization.py:75: error: Need type annotation for "local_arrays"  [var-annotated]

Tanjun (https://github.com/FasterSpeeding/Tanjun)
+ tanjun/hooks.py:301: error: Need type annotation for "results"  [var-annotated]
+ tanjun/dependencies/reloaders.py:228: error: <nothing> object is not iterable  [misc]
+ tanjun/dependencies/reloaders.py:229: error: Cannot determine type of "py_paths"  [has-type]
+ tanjun/dependencies/reloaders.py:230: error: Cannot determine type of "sys_paths"  [has-type]
+ tanjun/dependencies/reloaders.py:267: error: <nothing> object is not iterable  [misc]
+ tanjun/dependencies/reloaders.py:268: error: Cannot determine type of "path"  [has-type]
+ tanjun/dependencies/reloaders.py:268: error: Cannot determine type of "info"  [has-type]
+ tanjun/dependencies/reloaders.py:433: error: Need type annotation for "scan_result"  [var-annotated]
- tanjun/dependencies/reloaders.py:466: error: Incompatible types in assignment (expression has type "str", variable has type "Path")  [assignment]
- tanjun/dependencies/reloaders.py:469: error: Invalid index type "Path" for "Dict[str, _PyPathInfo]"; expected type "str"  [index]
+ tanjun/parsing.py:476: error: Need type annotation for "values"  [var-annotated]
+ tanjun/clients.py:2420: error: Need type annotation for "paths"  [var-annotated]
+ tanjun/clients.py:2510: error: Need type annotation for "module"  [var-annotated]
+ tanjun/clients.py:2625: error: Need type annotation for "module"  [var-annotated]

sublime_debugger (https://github.com/daveleroy/sublime_debugger)
+ modules/core/core.py:24: error: Return type "Generator[Any, None, T]" of "__await__" incompatible with return type "Generator[Any, None, _T]" in supertype "Future"
+ modules/core/core.py:27: error: Argument 1 of "set_result" is incompatible with supertype "Future"; supertype defines the argument type as "_T"
+ modules/core/core.py:27: note: This violates the Liskov substitution principle
+ modules/core/core.py:27: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#incompatible-overrides

aioredis (https://github.com/aio-libs/aioredis)
+ aioredis/connection.py:1493: error: Need type annotation for "resp"  [var-annotated]
+ aioredis/connection.py:1662: error: Need type annotation for "resp"  [var-annotated]

aiortc (https://github.com/aiortc/aiortc)
+ src/aiortc/rtcrtpsender.py:283: error: <nothing> object is not iterable
+ src/aiortc/rtcpeerconnection.py:720: error: <nothing> has no attribute "__iter__" (not iterable)

mypy_primer (https://github.com/hauntsaninja/mypy_primer)
+ mypy_primer.py:267: error: <nothing> object is not iterable
+ mypy_primer.py:273: error: <nothing> object is not iterable
+ mypy_primer.py:274: error: Cannot determine type of "new_mypy"
+ mypy_primer.py:275: error: Cannot determine type of "old_mypy"
+ mypy_primer.py:277: error: Cannot determine type of "new_version"
+ mypy_primer.py:278: error: Cannot determine type of "old_version"
+ mypy_primer.py:280: error: Cannot determine type of "new_mypy"
+ mypy_primer.py:280: error: Cannot determine type of "old_mypy"
+ mypy_primer.py:415: error: <nothing> object is not iterable
+ mypy_primer.py:419: error: Cannot determine type of "new_result"
+ mypy_primer.py:419: error: Cannot determine type of "old_result"
+ mypy_primer.py:596: error: Need type annotation for "recent_mypy_exes"
+ mypy_primer.py:624: error: Need type annotation for "results"
+ mypy_primer.py:641: error: Need type annotation for "results" (hint: "results: List[<type>] = ...")
+ mypy_primer.py:670: error: Need type annotation for "results_fut"
+ mypy_primer.py:719: error: Need type annotation for "all_paths"
+ mypy_primer.py:753: error: Need type annotation for "result"

paasta (https://github.com/yelp/paasta)
+ paasta_tools/drain_lib.py:322: error: Need type annotation for "res"
+ paasta_tools/instance/kubernetes.py:276: error: Need type annotation for "status"
+ paasta_tools/instance/kubernetes.py:326: error: Need type annotation for "pods"
+ paasta_tools/instance/kubernetes.py:566: note: Maybe you forgot to use "await"?
+ paasta_tools/instance/kubernetes.py:601: note: Maybe you forgot to use "await"?
- paasta_tools/instance/kubernetes.py:621: error: Argument "pod_status_by_sha_and_readiness_task" to "get_versions_for_controller_revisions" has incompatible type "Task[defaultdict[Tuple[str, str], defaultdict[bool, List[Future[Dict[str, Any]]]]]]"; expected "Future[Mapping[Tuple[str, str], Mapping[bool, Sequence[Future[Mapping[str, Any]]]]]]"
+ paasta_tools/instance/kubernetes.py:624: note: Maybe you forgot to use "await"?
- paasta_tools/instance/kubernetes.py:640: error: Argument "pod_status_by_replicaset_task" to "get_versions_for_replicasets" has incompatible type "Task[Dict[str, List[Future[Dict[str, Any]]]]]"; expected "Future[Mapping[str, Sequence[Future[Dict[str, Any]]]]]"
+ paasta_tools/instance/kubernetes.py:643: note: Maybe you forgot to use "await"?
+ paasta_tools/instance/kubernetes.py:691: error: Need type annotation for "pods"
+ paasta_tools/instance/kubernetes.py:723: error: Need type annotation for "pod_status_by_replicaset"
+ paasta_tools/instance/kubernetes.py:724: error: Need type annotation for "versions"
+ paasta_tools/instance/kubernetes.py:784: error: Need type annotation for "pod_event_messages"
+ paasta_tools/instance/kubernetes.py:798: error: <nothing> has no attribute "__iter__" (not iterable)
+ paasta_tools/instance/kubernetes.py:919: error: <nothing> object is not iterable
+ paasta_tools/instance/kubernetes.py:936: error: Cannot determine type of "previous_tail_lines"
+ paasta_tools/instance/kubernetes.py:940: error: Cannot determine type of "tail_lines"
+ paasta_tools/instance/kubernetes.py:958: error: <nothing> has no attribute "__iter__" (not iterable)
+ paasta_tools/instance/kubernetes.py:994: error: Need type annotation for "pod_status_by_sha_and_readiness"
+ paasta_tools/instance/kubernetes.py:995: error: Need type annotation for "versions"
+ paasta_tools/instance/kubernetes.py:1073: error: Need type annotation for "pod_list"
+ paasta_tools/cli/cmds/status.py:1422: error: Need type annotation for "jobs_details"
+ paasta_tools/cli/cmds/mark_for_deployment.py:1817: error: <nothing> object is not iterable
+ paasta_tools/cli/cmds/mark_for_deployment.py:1822: error: Cannot determine type of "instance"

black (https://github.com/psf/black)
+ src/black/concurrency.py:185:27: error: Need type annotation for "changed"  [var-annotated]
+ src/blackd/__init__.py:132:25: error: Need type annotation for "formatted_str"  [var-annotated]

kopf (https://github.com/nolar/kopf)
+ kopf/_cogs/aiokits/aiotasks.py:25: error: Type application has too few types (2 expected)
+ kopf/_cogs/clients/api.py:39: error: Need type annotation for "cert"
+ kopf/_core/reactor/running.py:440: error: Need type annotation for "result"

pyppeteer (https://github.com/pyppeteer/pyppeteer)
+ pyppeteer/worker.py:92: error: <nothing> has no attribute "evaluate"
+ pyppeteer/worker.py:100: error: <nothing> has no attribute "evaluateHandle"
+ pyppeteer/page.py:854: error: Value of type <nothing> is not indexable
+ pyppeteer/page.py:1006: error: Value of type <nothing> is not indexable
+ pyppeteer/network_manager.py:650: error: Need type annotation for "result"
+ pyppeteer/frame_manager.py:853: error: Need type annotation for "result"

core (https://github.com/home-assistant/core)
+ homeassistant/loader.py:187: error: Need type annotation for "dirs"  [var-annotated]
+ homeassistant/loader.py:191: error: Need type annotation for "integrations"  [var-annotated]
+ homeassistant/loader.py:782: error: Need type annotation for "integrations"  [var-annotated]
+ homeassistant/data_entry_flow.py:225: error: <nothing> object is not iterable  [misc]
+ homeassistant/data_entry_flow.py:230: error: Cannot determine type of "result"  [has-type]
+ homeassistant/data_entry_flow.py:231: error: Cannot determine type of "flow"  [has-type]
+ homeassistant/data_entry_flow.py:231: error: Cannot determine type of "result"  [has-type]
+ homeassistant/data_entry_flow.py:233: error: Returning Any from function declared to return "FlowResult"  [no-any-return]
+ homeassistant/data_entry_flow.py:233: error: Cannot determine type of "result"  [has-type]
+ homeassistant/requirements.py:204: error: Need type annotation for "results"  [var-annotated]
+ homeassistant/requirements.py:261: error: <nothing> object is not iterable  [misc]
+ homeassistant/requirements.py:264: error: Cannot determine type of "installed"  [has-type]
+ homeassistant/requirements.py:265: error: Cannot determine type of "failures"  [has-type]
+ homeassistant/requirements.py:266: error: Cannot determine type of "failures"  [has-type]
+ homeassistant/requirements.py:267: error: Cannot determine type of "failures"  [has-type]
+ homeassistant/helpers/trigger.py:156: error: Need type annotation for "attach_results"  [var-annotated]
+ homeassistant/helpers/translation.py:183: error: Need type annotation for "loaded_translations"  [var-annotated]
+ homeassistant/helpers/translation.py:232: error: <nothing> has no attribute "__iter__" (not iterable)  [attr-defined]
+ homeassistant/helpers/storage.py:57: error: Need type annotation for "config"  [var-annotated]
+ homeassistant/auth/__init__.py:51: error: Need type annotation for "providers"  [var-annotated]
+ homeassistant/auth/__init__.py:66: error: Need type annotation for "modules"  [var-annotated]
+ homeassistant/config.py:348: error: Need type annotation for "config"  [var-annotated]
+ homeassistant/config.py:356: error: Returning Any from function declared to return "Dict[Any, Any]"  [no-any-return]
+ homeassistant/helpers/service.py:473: error: Need type annotation for "contents"  [var-annotated]
+ homeassistant/helpers/service.py:492: error: Unused "type: ignore" comment
+ homeassistant/setup.py:127: error: Need type annotation for "results"  [var-annotated]
+ homeassistant/helpers/check_config.py:84: error: Cannot determine type of "core_config"  [has-type]
+ homeassistant/helpers/check_config.py:95: error: Statement is unreachable  [unreachable]
+ homeassistant/helpers/check_config.py:97: error: Statement is unreachable  [unreachable]
+ homeassistant/helpers/check_config.py:110: error: Statement is unreachable  [unreachable]
+ homeassistant/components/http/ban.py:212: error: Need type annotation for "list_"  [var-annotated]
+ homeassistant/helpers/restore_state.py:288: error: <nothing> object is not iterable  [misc]
+ homeassistant/helpers/restore_state.py:292: error: Cannot determine type of "data"  [has-type]
+ homeassistant/helpers/restore_state.py:296: error: <nothing> object is not iterable  [misc]
+ homeassistant/helpers/restore_state.py:300: error: Cannot determine type of "data"  [has-type]
+ homeassistant/components/device_automation/__init__.py:215: error: Unused "type: ignore" comment
+ homeassistant/components/device_automation/__init__.py:256: error: <nothing> has no attribute "__iter__" (not iterable)  [attr-defined]
+ homeassistant/helpers/collection.py:418: error: <nothing> has no attribute "__iter__" (not iterable)  [attr-defined]
+ homeassistant/components/system_health/__init__.py:103: error: Need type annotation for "domain_data"  [var-annotated]
+ homeassistant/components/device_tracker/legacy.py:327: error: <nothing> has no attribute "__iter__" (not iterable)  [attr-defined]
+ homeassistant/components/device_tracker/legacy.py:913: error: Need type annotation for "devices"  [var-annotated]
+ homeassistant/helpers/script.py:1018: error: Need type annotation for "results"  [var-annotated]
+ homeassistant/components/recorder/websocket_api.py:191: error: Need type annotation for "statistic_ids"  [var-annotated]
+ homeassistant/components/recorder/websocket_api.py:230: error: Need type annotation for "statistic_ids"  [var-annotated]
+ homeassistant/components/recorder/websocket_api.py:286: error: Need type annotation for "metadatas"  [var-annotated]
+ homeassistant/components/hassio/__init__.py:982: error: <nothing> object is not iterable  [misc]
+ homeassistant/components/hassio/__init__.py:994: error: Need type annotation for "stats_data"  [var-annotated]
+ homeassistant/components/ssdp/__init__.py:392: error: Need type annotation for "results"  [var-annotated]
+ homeassistant/components/zeroconf/__init__.py:191: error: <nothing> object is not iterable  [misc]
+ homeassistant/components/zeroconf/__init__.py:194: error: Cannot determine type of "zeroconf_types"  [has-type]
+ homeassistant/components/zeroconf/__init__.py:194: error: Cannot determine type of "homekit_models"  [has-type]
+ homeassistant/components/bluetooth/manager.py:175: error: Need type annotation for "scanner_diagnostics"  [var-annotated]
+ homeassistant/helpers/config_entry_flow.py:77: error: Statement is unreachable  [unreachable]
+ homeassistant/components/yeelight/scanner.py:86: error: Need type annotation for "results"  [var-annotated]
+ homeassistant/components/yale_smart_alarm/coordinator.py:36: error: Need type annotation for "updates"  [var-annotated]
+ homeassistant/components/xiaomi_aqara/__init__.py:153: error: Need type annotation for "xiaomi_gateway"  [var-annotated]
+ homeassistant/components/wiz/discovery.py:27: error: Need type annotation for "discovered"  [var-annotated]
+ homeassistant/components/wilight/parent_device.py:43: error: Need type annotation for "api_device"  [var-annotated]
+ homeassistant/components/verisure/coordinator.py:67: error: Need type annotation for "overview"  [var-annotated]
+ homeassistant/components/venstar/config_flow.py:47: error: Need type annotation for "info_success"  [var-annotated]
+ homeassistant/components/upnp/device.py:39: error: Need type annotation for "mac_address"  [var-annotated]
+ homeassistant/components/upnp/device.py:151: error: Need type annotation for "values"  [var-annotated]
+ homeassistant/components/upnp/device.py:170: error: Need type annotation for "values"  [var-annotated]
+ homeassistant/components/upcloud/__init__.py:87: error: <nothing> has no attribute "__iter__" (not iterable)  [attr-defined]
+ homeassistant/components/tractive/__init__.py:91: error: Need type annotation for "trackables"  [var-annotated]
+ homeassistant/components/tractive/__init__.py:130: error: <nothing> object is not iterable  [misc]
+ homeassistant/components/tractive/__init__.py:134: error: Cannot determine type of "tracker_details"  [has-type]
+ homeassistant/components/tractive/__init__.py:134: error: Cannot determine type of "hw_info"  [has-type]
+ homeassistant/components/tractive/__init__.py:134: error: Cannot determine type of "pos_report"  [has-type]
+ homeassistant/components/totalconnect/__init__.py:44: error: Need type annotation for "client"  [var-annotated]
+ homeassistant/components/tolo/config_flow.py:50: error: Need type annotation for "device_available"  [var-annotated]
+ homeassistant/components/tolo/config_flow.py:73: error: Need type annotation for "device_available"  [var-annotated]
+ homeassistant/components/tellduslive/__init__.py:62: error: Need type annotation for "session"  [var-annotated]
+ homeassistant/components/tautulli/coordinator.py:54: error: <nothing> object is not iterable  [misc]
+ homeassistant/components/tankerkoenig/__init__.py:128: error: Need type annotation for "setup_ok"  [var-annotated]
+ homeassistant/components/tankerkoenig/__init__.py:224: error: Need type annotation for "data"  [var-annotated]
+ homeassistant/components/synology_dsm/config_flow.py:180: error: Need type annotation for "serial"  [var-annotated]
+ homeassistant/components/steamist/discovery.py:69: error: Need type annotation for "discovered"  [var-annotated]
+ homeassistant/components/steam_online/config_flow.py:51: error: Need type annotation for "res"  [var-annotated]
+ homeassistant/components/steam_online/config_flow.py:145: error: <nothing> has no attribute "__iter__" (not iterable)  [attr-defined]
+ homeassistant/components/spider/sensor.py:25: error: <nothing> has no attribute "__iter__" (not iterable)  [attr-defined]
+ homeassistant/components/spider/__init__.py:58: error: Need type annotation for "api"  [var-annotated]
+ homeassistant/components/soundtouch/config_flow.py:91: error: Need type annotation for "device"  [var-annotated]
+ homeassistant/components/soundtouch/__init__.py:130: error: Need type annotation for "device"  [var-annotated]
+ homeassistant/components/soma/__init__.py:58: error: Need type annotation for "devices"  [var-annotated]
+ homeassistant/components/simplisafe/__init__.py:689: error: Need type annotation for "results"  [var-annotated]
+ homeassistant/components/roomba/__init__.py:46: error: Need type annotation for "roomba"  [var-annotated]
+ homeassistant/components/ridwell/__init__.py:64: error: Need type annotation for "results"  [var-annotated]
+ homeassistant/components/powerwall/config_flow.py:49: error: <nothing> object is not iterable  [misc]
+ homeassistant/components/powerwall/config_flow.py:58: error: Cannot determine type of "site_info"  [has-type]
+ homeassistant/components/powerwall/config_flow.py:58: error: Cannot determine type of "gateway_din"  [has-type]
+ homeassistant/components/nuki/__init__.py:89: error: Need type annotation for "bridge"  [var-annotated]
+ homeassistant/components/nuki/__init__.py:98: error: <nothing> object is not iterable  [misc]
+ homeassistant/components/nuki/__init__.py:143: error: Cannot determine type of "locks"  [has-type]
+ homeassistant/components/nuki/__init__.py:144: error: Cannot determine type of "openers"  [has-type]
+ homeassistant/components/nuheat/config_flow.py:50: error: Need type annotation for "thermostat"  [var-annotated]
+ homeassistant/components/nuheat/__init__.py:41: error: Need type annotation for "thermostat"  [var-annotated]
+ homeassistant/components/notion/__init__.py:68: error: Need type annotation for "results"  [var-annotated]
+ homeassistant/components/nissan_leaf/__init__.py:131: error: Need type annotation for "result"  [var-annotated]
+ homeassistant/components/nissan_leaf/__init__.py:359: error: Need type annotation for "start_server_info"  [var-annotated]
+ homeassistant/components/nissan_leaf/__init__.py:370: error: Need type annotation for "request"  [var-annotated]
+ homeassistant/components/nissan_leaf/__init__.py:387: error: Need type annotation for "check_result_info"  [var-annotated]
+ homeassistant/components/nissan_leaf/__init__.py:394: error: Need type annotation for "server_info"  [var-annotated]
+ homeassistant/components/nissan_leaf/__init__.py:454: error: Need type annotation for "request"  [var-annotated]
+ homeassistant/components/nissan_leaf/__init__.py:470: error: Statement is unreachable  [unreachable]
+ homeassistant/components/nissan_leaf/__init__.py:473: error: Statement is unreachable  [unreachable]
+ homeassistant/components/monoprice/__init__.py:30: error: Need type annotation for "monoprice"  [var-annotated]
+ homeassistant/components/modem_callerid/config_flow.py:37: error: Need type annotation for "dev_path"  [var-annotated]
+ homeassistant/components/modem_callerid/config_flow.py:66: error: Need type annotation for "ports"  [var-annotated]
+ homeassistant/components/modem_callerid/config_flow.py:87: error: Need type annotation for "dev_path"  [var-annotated]
+ homeassistant/components/lifx/util.py:172: error: Statement is unreachable  [unreachable]
+ homeassistant/components/jellyfin/client_wrapper.py:30: error: Need type annotation for "userid"  [var-annotated]
+ homeassistant/components/jellyfin/client_wrapper.py:34: error: Returning Any from function declared to return "str"  [no-any-return]
+ homeassistant/components/iqvia/__init__.py:91: error: Need type annotation for "results"  [var-annotated]
+ homeassistant/components/ios/__init__.py:256: error: Need type annotation for "ios_config"  [var-annotated]
+ homeassistant/components/honeywell/__init__.py:54: error: Need type annotation for "client"  [var-annotated]
+ homeassistant/components/google_sheets/config_flow.py:92: error: Need type annotation for "doc"  [var-annotated]
+ homeassistant/components/github/config_flow.py:44: error: Need type annotation for "results"  [var-annotated]
+ homeassistant/components/github/config_flow.py:62: error: Need type annotation for "results"  [var-annotated]
+ homeassistant/components/fritzbox_callmonitor/config_flow.py:119: error: Need type annotation for "phonebook_info"  [var-annotated]
+ homeassistant/components/fritzbox_callmonitor/config_flow.py:156: error: Need type annotation for "result"  [var-annotated]
+ homeassistant/components/fritzbox/config_flow.py:107: error: Need type annotation for "result"  [var-annotated]
+ homeassistant/components/fritzbox/config_flow.py:163: error: Need type annotation for "result"  [var-annotated]
+ homeassistant/components/fritzbox/config_flow.py:200: error: Need type annotation for "result"  [var-annotated]
+ homeassistant/components/fritz/config_flow.py:94: error: Need type annotation for "current_host"  [var-annotated]
+ homeassistant/components/fritz/config_flow.py:99: error: Need type annotation for "entry_host"  [var-annotated]
+ homeassistant/components/fritz/config_flow.py:173: error: Need type annotation for "error"  [var-annotated]
+ homeassistant/components/fritz/config_flow.py:223: error: Need type annotation for "error"  [var-annotated]
+ homeassistant/components/fritz/config_flow.py:224: error: Statement is unreachable  [unreachable]
+ homeassistant/components/fritz/config_flow.py:229: error: Statement is unreachable  [unreachable]
+ homeassistant/components/fritz/config_flow.py:272: error: Need type annotation for "error"  [var-annotated]
+ homeassistant/components/fritz/config_flow.py:273: error: Statement is unreachable  [unreachable]
+ homeassistant/components/fritz/config_flow.py:277: error: Statement is unreachable  [unreachable]
+ homeassistant/components/flipr/config_flow.py:91: error: Need type annotation for "flipr_ids"  [var-annotated]
+ homeassistant/components/firmata/__init__.py:221: error: Need type annotation for "results" (hint: "results: List[<type>] = ...")  [var-annotated]
+ homeassistant/components/filesize/sensor.py:71: error: Need type annotation for "get_path"  [var-annotated]
+ homeassistant/components/filesize/sensor.py:99: error: Need type annotation for "statinfo"  [var-annotated]
+ homeassistant/components/filesize/__init__.py:25: error: Need type annotation for "check_file"  [var-annotated]
+ homeassistant/components/file_upload/__init__.py:80: error: Need type annotation for "temp_dir"  [var-annotated]
+ homeassistant/components/fibaro/__init__.py:497: error: Need type annotation for "controller"  [var-annotated]
+ homeassistant/components/ffmpeg/__init__.py:122: error: Need type annotation for "image"  [var-annotated]
+ homeassistant/components/elkm1/discovery.py:54: error: Need type annotation for "discovered"  [var-annotated]
+ homeassistant/components/dunehd/config_flow.py:25: error: Need type annotation for "state"  [var-annotated]
+ homeassistant/components/dsmr/sensor.py:446: error: <nothing> object is not iterable  [misc]
+ homeassistant/components/dsmr/sensor.py:450: error: Statement is unreachable  [unreachable]
+ homeassistant/components/dsmr/sensor.py:493: error: Right operand of "and" is never evaluated  [unreachable]
+ homeassistant/components/dsmr/sensor.py:495: error: Statement is unreachable  [unreachable]
+ homeassistant/components/dsmr/sensor.py:498: error: Statement is unreachable  [unreachable]
+ homeassistant/components/dsmr/sensor.py:501: error: Statement is unreachable  [unreachable]
+ homeassistant/components/dsmr/config_flow.py:84: error: Cannot determine type of "transport"  [has-type]
+ homeassistant/components/dsmr/config_flow.py:88: error: Cannot determine type of "transport"  [has-type]
+ homeassistant/components/dsmr/config_flow.py:117: error: <nothing> object is not iterable  [misc]
+ homeassistant/components/dsmr/config_flow.py:122: error: Cannot determine type of "transport"  [has-type]
+ homeassistant/components/dsmr/config_flow.py:125: error: Cannot determine type of "protocol"  [has-type]
+ homeassistant/components/dsmr/config_flow.py:128: error: Cannot determine type of "transport"  [has-type]
+ homeassistant/components/dsmr/config_flow.py:129: error: Cannot determine type of "protocol"  [has-type]
+ homeassistant/components/dsmr/config_flow.py:225: error: Need type annotation for "dev_path"  [var-annotated]
+ homeassistant/components/dsmr/config_flow.py:238: error: Need type annotation for "ports"  [var-annotated]
+ homeassistant/components/doorbird/config_flow.py:45: error: <nothing> object is not iterable  [misc]
+ homeassistant/components/doorbird/config_flow.py:53: error: Cannot determine type of "status"  [has-type]
+ homeassistant/components/doorbird/config_flow.py:56: error: Cannot determine type of "info"  [has-type]
+ homeassistant/components/doorbird/__init__.py:110: error: <nothing> object is not iterable  [misc]
+ homeassistant/components/doorbird/__init__.py:122: error: Cannot determine type of "status"  [has-type]
+ homeassistant/components/doorbird/__init__.py:127: error: Cannot determine type of "status"  [has-type]
+ homeassistant/components/doorbird/__init__.py:145: error: Cannot determine type of "info"  [has-type]
+ homeassistant/components/dnsip/config_flow.py:61: error: Need type annotation for "tasks"  [var-annotated]
+ homeassistant/components/dexcom/__init__.py:31: error: Need type annotation for "dexcom"  [var-annotated]
+ homeassistant/components/devolo_home_control/__init__.py:35: error: Need type annotation for "credentials_valid"  [var-annotated]
+ homeassistant/components/devolo_home_control/__init__.py:43: error: Statement is unreachable  [unreachable]
+ homeassistant/components/demo/__init__.py:172: error: Need type annotation for "results"  [var-annotated]
+ homeassistant/components/demo/__init__.py:269: error: Need type annotation for "last_stats"  [var-annotated]
+ homeassistant/components/deluge/coordinator.py:41: error: Need type annotation for "_data"  [var-annotated]
+ homeassistant/components/cpuspeed/config_flow.py:32: error: Statement is unreachable  [unreachable]
+ homeassistant/components/cpuspeed/config_flow.py:34: error: Statement is unreachable  [unreachable]
+ homeassistant/components/cpuspeed/__init__.py:13: error: Statement is unreachable  [unreachable]
+ homeassistant/components/cpuspeed/__init__.py:19: error: Statement is unreachable  [unreachable]
+ homeassistant/components/coinbase/__init__.py:71: error: Need type annotation for "instance"  [var-annotated]
+ homeassistant/components/co2signal/__init__.py:78: error: Need type annotation for "data"  [var-annotated]
+ homeassistant/components/bosch_shc/__init__.py:38: error: Need type annotation for "session"  [var-annotated]
+ homeassistant/components/bluetooth_tracker/device_tracker.py:160: error: Need type annotation for "devices"  [var-annotated]
+ homeassistant/components/bluetooth_tracker/device_tracker.py:166: error: Need type annotation for "friendly_name"  [var-annotated]
+ homeassistant/components/backup/manager.py:82: error: Need type annotation for "backups"  [var-annotated]
+ homeassistant/components/backup/manager.py:160: error: Need type annotation for "pre_backup_results"  [var-annotated]
+ homeassistant/components/backup/manager.py:208: error: Need type annotation for "post_backup_results"  [var-annotated]
+ homeassistant/components/aws/__init__.py:135: error: Need type annotation for "results"  [var-annotated]
+ homeassistant/components/aurora_abb_powerone/config_flow.py:91: error: Need type annotation for "result"  [var-annotated]
+ homeassistant/components/aurora_abb_powerone/config_flow.py:99: error: Need type annotation for "info"  [var-annotated]
+ homeassistant/components/androidtv/__init__.py:97: error: <nothing> object is not iterable  [misc]
+ homeassistant/components/androidtv/__init__.py:104: error: Cannot determine type of "adbkey"  [has-type]
+ homeassistant/components/androidtv/__init__.py:110: error: Cannot determine type of "signer"  [has-type]
+ homeassistant/components/androidtv/__init__.py:122: error: Cannot determine type of "adb_log"  [has-type]
+ homeassistant/components/abode/__init__.py:94: error: Need type annot

... (truncated 282 lines) ...

github-actions[bot] avatar Sep 21 '22 18:09 github-actions[bot]

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

kopf (https://github.com/nolar/kopf)
+ kopf/_cogs/aiokits/aiotasks.py:25: error: Type application has too few types (2 expected)

github-actions[bot] avatar Sep 21 '22 18:09 github-actions[bot]

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

paasta (https://github.com/yelp/paasta)
- paasta_tools/instance/kubernetes.py:621: error: Argument "pod_status_by_sha_and_readiness_task" to "get_versions_for_controller_revisions" has incompatible type "Task[defaultdict[Tuple[str, str], defaultdict[bool, List[Future[Dict[str, Any]]]]]]"; expected "Future[Mapping[Tuple[str, str], Mapping[bool, Sequence[Future[Mapping[str, Any]]]]]]"
- paasta_tools/instance/kubernetes.py:640: error: Argument "pod_status_by_replicaset_task" to "get_versions_for_replicasets" has incompatible type "Task[Dict[str, List[Future[Dict[str, Any]]]]]"; expected "Future[Mapping[str, Sequence[Future[Dict[str, Any]]]]]"

kopf (https://github.com/nolar/kopf)
+ kopf/_cogs/aiokits/aiotasks.py:25: error: Type application has too few types (2 expected)

github-actions[bot] avatar Sep 21 '22 18:09 github-actions[bot]

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

kopf (https://github.com/nolar/kopf)
+ kopf/_cogs/aiokits/aiotasks.py:25: error: Type application has too few types (2 expected)

github-actions[bot] avatar Sep 22 '22 16:09 github-actions[bot]

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

github-actions[bot] avatar Sep 22 '22 16:09 github-actions[bot]

I'm still hesitant about having a covariant subclass of an invariant base class. Given B <: A, we'd have Task[A] <: Future[A], Task[B] <: Future[B], and Task[B] <: Task[A], but not Future[B] <: Future[A]. That feels unsound.

@erictraut curious if you have any opinions here: maybe type checkers should not allow covariant subclasses of invariant base classes? And how would this work under PEP 695?

JelleZijlstra avatar Sep 22 '22 17:09 JelleZijlstra

It definitely feels sketchy, but in this particular case as far as I can tell it's not any more unsound than the existing Liskov issue with set_result

hauntsaninja avatar Sep 22 '22 20:09 hauntsaninja

I agree this is sketchy. Neither mypy nor pyright (I haven't tried the other type checkers) complain about this situation currently, even though it's clearly wrong.

from typing import TypeVar

T = TypeVar("T", covariant=True)

class MyList(list[T]): pass

def append_float(y: MyList[float]):
    y.append(1.0)

x: MyList[int] = MyList([1, 2, 3])
append_float(x)

This is a pretty big hole in type safety, and it should probably be plugged. I'll explore a solution in pyright and see whether it produces a lot of noise for internal Microsoft code bases that use pyright.

In the meantime, I recommend not taking a dependency on this sketchy behavior — especially for classes that are as commonly-used as Task and Future.

erictraut avatar Sep 22 '22 20:09 erictraut

I've implemented support for this missing check in pyright. I didn't see any violations of it in our internal Microsoft source bases, nor were there any existing violations in typeshed.

I'm not sure what I'd recommend for this PR. I understand the reasoning, but I think I'd reject it because it's unsound from a type perspective, and it will no longer pass type checking when using pyright. If you decide to keep it, you can use a # type: ignore or # pyright: ignore comment to suppress the error.

erictraut avatar Sep 23 '22 03:09 erictraut

Thanks for checking that!

I'm still in favour of this PR because it solves a real problem, models the runtime better, and there isn't opportunity for unsoundness beyond the pre-existing Liskov violation. Seems worth adding a test case to confirm behaviour, though.

But it is icky and we've managed so far without it, so if we were to reject that would be understandable.

Edit: I added the check to mypy in https://github.com/python/mypy/pull/13714

hauntsaninja avatar Sep 23 '22 04:09 hauntsaninja

I don't have a strong opinion on this, but I agree that this can be useful. In one project, I had many collections of tasks that I wanted to cancel when a specific thing happened, so I made it something like list[asyncio.Task[Any]]. But because I never wanted to access the results of any of these tasks, and in fact I would want an error if I assume the results are of any specific type, asyncio.Task[object] would have been better if it worked.

Akuli avatar Sep 23 '22 19:09 Akuli

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

github-actions[bot] avatar Sep 28 '22 13:09 github-actions[bot]

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

github-actions[bot] avatar Sep 28 '22 13:09 github-actions[bot]

This has been open for a bit and while it is a sketchy change, no one seems particularly opposed to it.

So I'm planning on merging. tldr:

  • solves a real problem encountered by multiple people
  • models the runtime better
  • there isn't opportunity for unsoundness beyond the pre-existing Liskov violation
  • two maintainers in favour, no maintainers clearly opposed

hauntsaninja avatar Oct 03 '22 21:10 hauntsaninja