Skip to content
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

checkexpr: infer union type for ternaries #12712

Closed
wants to merge 2 commits into from

Conversation

huguesb
Copy link
Contributor

@huguesb huguesb commented May 1, 2022

This fails a few tests, most of which seems straightforwardly fixable

Pushing out to get mypy_primer output

Deeply nested if/else expressions have a worst-case exponential behavior.

This will for instance manifest when returning literal values which cause
repeated analysis of conditional branches with subtly different type context
for each literal.

This can be optimized by observing that a simple literal context will yield
the same analysis as its fallback type, and likewise, two literals of the
same fallback type will yield the same analysis. In those case we can avoid
the repeated analysis and prevent the worst-case exponential behavior.

Fixes python#9591
This fails a few tests, most of which seems straightforwardly fixable

Pushing out to get mypy_primer output
@huguesb huguesb changed the title pr ternary union checkexpr: infer union type for ternaries May 1, 2022
@huguesb huguesb marked this pull request as draft May 1, 2022 20:45
@github-actions
Copy link
Contributor

github-actions bot commented May 1, 2022

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

ignite (https://github.com/pytorch/ignite)
+ ignite/metrics/precision.py:37: error: Need type annotation for "init_value"  [var-annotated]
+ ignite/metrics/fbeta.py:154: error: Unused "type: ignore" comment
+ ignite/metrics/fbeta.py:163: error: Unused "type: ignore" comment
+ ignite/handlers/time_profilers.py:417: error: Incompatible types in string interpolation (expression has type "str", placeholder has type "Union[int, float]")  [str-format]

graphql-core (https://github.com/graphql-python/graphql-core)
+ src/graphql/language/visitor.py:291: error: Incompatible types in assignment (expression has type "Union[Any, Tuple[str, ...]]", variable has type "Tuple[Node, ...]")
+ src/graphql/language/parser.py:383: error: Incompatible types in assignment (expression has type "Callable[[], ArgumentNode]", variable has type "Union[Callable[[], ConstArgumentNode], Callable[[bool], ArgumentNode]]")
+ src/graphql/execution/subscribe.py:89: error: Incompatible return value type (got "Union[Any, Awaitable[ExecutionResult], ExecutionResult]", expected "ExecutionResult")
+ tests/execution/test_abstract.py:45: error: Unused "type: ignore" comment

bidict (https://github.com/jab/bidict)
+ bidict/_base.py:539: error: Unused "type: ignore" comment

psycopg (https://github.com/psycopg/psycopg)
+ psycopg/psycopg/waiting.py:65: error: Incompatible types in assignment (expression has type "Optional[Any]", variable has type "RV")  [assignment]
+ psycopg/psycopg/waiting.py:97: error: Incompatible types in assignment (expression has type "Optional[Any]", variable has type "RV")  [assignment]
+ psycopg/psycopg/waiting.py:147: error: Incompatible types in assignment (expression has type "Optional[Any]", variable has type "RV")  [assignment]
+ psycopg/psycopg/waiting.py:203: error: Incompatible types in assignment (expression has type "Optional[Any]", variable has type "RV")  [assignment]
+ psycopg/psycopg/waiting.py:242: error: Incompatible types in assignment (expression has type "Optional[Any]", variable has type "RV")  [assignment]

rotki (https://github.com/rotki/rotki)
+ rotkehlchen/chain/ethereum/modules/nfts.py:204: error: Argument 1 to "Asset" has incompatible type "Union[Any, Asset]"; expected "str"
+ rotkehlchen/globaldb/updates.py:424: error: Unsupported operand types for <= ("int" and "None")
+ rotkehlchen/globaldb/updates.py:424: note: Right operand is of type "Optional[Any]"
+ rotkehlchen/tests/utils/rotkehlchen.py:209: error: Argument "poloniex_patch" to "BalancesTestSetup" has incompatible type "Optional[Any]"; expected "_patch[Any]"
+ rotkehlchen/tests/utils/rotkehlchen.py:210: error: Argument "binance_patch" to "BalancesTestSetup" has incompatible type "Optional[Any]"; expected "_patch[Any]"
- rotkehlchen/api/v1/resources.py:1895: error: Untyped decorator makes function "put" untyped
- rotkehlchen/api/v1/resources.py:1910: error: Untyped decorator makes function "patch" untyped
- rotkehlchen/api/v1/resources.py:1925: error: Untyped decorator makes function "get" untyped
- rotkehlchen/api/v1/resources.py:1929: error: Untyped decorator makes function "delete" untyped
- rotkehlchen/api/v1/resources.py:1945: error: Untyped decorator makes function "get" untyped
- rotkehlchen/api/v1/resources.py:1949: note: (Skipping most remaining errors due to unresolved imports or missing stubs; fix these first)
+ rotkehlchen/api/v1/resources.py:1895: note: (Skipping most remaining errors due to unresolved imports or missing stubs; fix these first)

pandera (https://github.com/pandera-dev/pandera)
+ pandera/checks.py:447: error: Item "bool" of "Union[Any, bool]" has no attribute "all"  [union-attr]
+ pandera/checks.py:449: error: Item "bool" of "Union[Any, bool]" has no attribute "all"  [union-attr]
+ pandera/model.py:341: error: Argument "checks" to "Column" has incompatible type "List[Check]"; expected "Union[Check, Hypothesis, List[Union[Check, Hypothesis]], None]"  [arg-type]
+ pandera/model.py:341: note: "List" is invariant -- see https://mypy.readthedocs.io/en/stable/common_issues.html#variance
+ pandera/model.py:341: note: Consider using "Sequence" instead, which is covariant
+ pandera/model.py:364: error: Argument "checks" to "Index" has incompatible type "List[Check]"; expected "Union[Check, Hypothesis, List[Union[Check, Hypothesis]], None]"  [arg-type]
+ pandera/model.py:364: note: "List" is invariant -- see https://mypy.readthedocs.io/en/stable/common_issues.html#variance
+ pandera/model.py:364: note: Consider using "Sequence" instead, which is covariant

vision (https://github.com/pytorch/vision)
+ torchvision/utils.py:248: error: Unsupported operand types for + ("int" and "Tuple[int]")  [operator]
+ torchvision/utils.py:248: note: Left operand is of type "Union[Any, Tuple[int, int, int], int]"
+ torchvision/prototype/datasets/utils/_internal.py:210: error: Incompatible return value type (got "List[Union[str, List[str]]]", expected "List[Union[str, Sequence[str]]]")  [return-value]
+ torchvision/prototype/datasets/utils/_internal.py:210: note: "List" is invariant -- see https://mypy.readthedocs.io/en/stable/common_issues.html#variance
+ torchvision/prototype/datasets/utils/_internal.py:210: note: Consider using "Sequence" instead, which is covariant
+ torchvision/prototype/datasets/utils/_internal.py:210: note: Perhaps you need a type annotation for "rows'"? Suggestion: "List[Union[str, Sequence[str]]]"
+ torchvision/datasets/cityscapes.py:194: error: Incompatible types in assignment (expression has type "Union[Tuple[Any, ...], Any]", variable has type "Dict[str, Any]")  [assignment]

prefect (https://github.com/PrefectHQ/prefect)
+ src/prefect/engine/state.py:906: error: Incompatible types in assignment (expression has type "List[State]", base class "State" defined the type as "List[Optional[Any]]")
+ src/prefect/engine/state.py:906: note: "List" is invariant -- see https://mypy.readthedocs.io/en/stable/common_issues.html#variance
+ src/prefect/engine/state.py:906: note: Consider using "Sequence" instead, which is covariant
+ src/prefect/backend/flow_run.py:560: error: Argument "run_config" to "FlowRunView" has incompatible type "Optional[Any]"; expected "RunConfig"

steam.py (https://github.com/Gobot1234/steam.py)
- steam/image.py:88: error: Incompatible types in assignment (expression has type "object", variable has type "ImageIO")  [assignment]
- steam/enums.py:134: error: Incompatible return value type (got "object", expected "bool")  [return-value]
+ steam/clan.py:534: error: Parameter 1 of Literal[...] is invalid  [misc]
+ steam/clan.py:555: error: Variable "steam.enums.ClanEvent.Game" is not valid as a type  [valid-type]
- steam/clan.py:534: note: (Skipping most remaining errors due to unresolved imports or missing stubs; fix these first)
+ steam/clan.py:555: note: (Skipping most remaining errors due to unresolved imports or missing stubs; fix these first)

werkzeug (https://github.com/pallets/werkzeug)
+ src/werkzeug/routing.py:1023: error: Incompatible return value type (got "List[Union[stmt, Str]]", expected "List[AST]")  [return-value]
+ src/werkzeug/routing.py:1023: note: "List" is invariant -- see https://mypy.readthedocs.io/en/stable/common_issues.html#variance
+ src/werkzeug/routing.py:1023: note: Consider using "Sequence" instead, which is covariant
+ src/werkzeug/routing.py:1023: note: Perhaps you need a type annotation for "ret"? Suggestion: "List[AST]"
- tests/test_wrappers.py:1078: error: Function is missing a return type annotation  [no-untyped-def]
- tests/test_wrappers.py:1078: note: Use "-> None" if function does not return a value
- tests/test_wrappers.py:1084: error: Function is missing a return type annotation  [no-untyped-def]
- tests/test_wrappers.py:1084: note: Use "-> None" if function does not return a value
- tests/test_wrappers.py:1109: error: Function is missing a return type annotation  [no-untyped-def]
- tests/test_wrappers.py:1109: note: Use "-> None" if function does not return a value
- tests/test_wrappers.py:1123: error: Function is missing a return type annotation  [no-untyped-def]
- tests/test_wrappers.py:1123: note: Use "-> None" if function does not return a value
- tests/test_wrappers.py:1132: note: (Skipping most remaining errors due to unresolved imports or missing stubs; fix these first)
+ tests/test_wrappers.py:1078: note: (Skipping most remaining errors due to unresolved imports or missing stubs; fix these first)

bandersnatch (https://github.com/pypa/bandersnatch)
+ src/bandersnatch_storage_plugins/swift.py: note: In member "open_file" of class "SwiftStorage":
+ src/bandersnatch_storage_plugins/swift.py:768: error: Argument 1 to "StringIO" has incompatible type "Union[str, bytes]"; expected "Optional[str]"
+ src/bandersnatch_storage_plugins/swift.py:768: error: Argument 1 to "BytesIO" has incompatible type "Union[str, bytes]"; expected "bytes"

pytest (https://github.com/pytest-dev/pytest)
+ src/_pytest/capture.py:598: error: Argument 1 to "CaptureResult" has incompatible type "Union[Any, str]"; expected "bytes"  [arg-type]
+ src/_pytest/capture.py:598: error: Argument 2 to "CaptureResult" has incompatible type "Union[Any, str]"; expected "bytes"  [arg-type]

schemathesis (https://github.com/schemathesis/schemathesis)
+ src/schemathesis/extra/pytest_plugin.py: note: In member "_parametrize" of class "SchemathesisCase":
+ src/schemathesis/extra/pytest_plugin.py:183: error: Item "None" of "Optional[Any]" has no attribute "pytest_generate_tests"

pyinstrument (https://github.com/joerick/pyinstrument)
- pyinstrument/renderers/console.py:77: error: "Type[object]" has no attribute "end"
- pyinstrument/renderers/console.py:79: error: "Type[object]" has no attribute "end"
- pyinstrument/renderers/console.py:79: error: "Type[object]" has no attribute "faint"
- pyinstrument/renderers/console.py:93: error: "Type[object]" has no attribute "faint"
- pyinstrument/renderers/console.py:93: error: "Type[object]" has no attribute "end"
- pyinstrument/renderers/console.py:123: error: "Type[object]" has no attribute "red"
- pyinstrument/renderers/console.py:125: error: "Type[object]" has no attribute "yellow"
- pyinstrument/renderers/console.py:127: error: "Type[object]" has no attribute "green"
- pyinstrument/renderers/console.py:129: error: "Type[object]" has no attribute "bright_green"
- pyinstrument/renderers/console.py:129: error: "Type[object]" has no attribute "faint"
- pyinstrument/renderers/console.py:133: error: "Type[object]" has no attribute "bg_dark_blue_255"
- pyinstrument/renderers/console.py:133: error: "Type[object]" has no attribute "white_255"

core (https://github.com/home-assistant/core)
+ homeassistant/data_entry_flow.py:99: error: Incompatible types (expression has type "Optional[Any]", TypedDict item "step_id" has type "str")  [typeddict-item]
+ homeassistant/helpers/translation.py:266: error: Unused "type: ignore" comment
- homeassistant/components/shelly/__init__.py:436: error: Returning Any from function declared to return "str"  [no-any-return]
- homeassistant/components/shelly/__init__.py:723: error: Returning Any from function declared to return "str"  [no-any-return]
+ homeassistant/components/keenetic_ndms2/device_tracker.py:129: error: Incompatible return value type (got "Optional[Any]", expected "str")  [return-value]
+ homeassistant/components/geniushub/sensor.py:97: error: Incompatible return value type (got "Union[Any, int]", expected "str")  [return-value]
+ homeassistant/components/fritz/switch.py:574: error: Incompatible types in assignment (expression has type "Optional[Any]", target has type "str")  [assignment]
- homeassistant/components/nest/diagnostics.py:74: error: Returning Any from function declared to return "Dict[Any, Any]"  [no-any-return]
- homeassistant/components/knx/climate.py:170: error: Returning Any from function declared to return "float"  [no-any-return]
- homeassistant/components/knx/climate.py:176: error: Returning Any from function declared to return "float"  [no-any-return]
+ homeassistant/components/heos/media_player.py:385: error: Incompatible return value type (got "Optional[Any]", expected "str")  [return-value]

pandas (https://github.com/pandas-dev/pandas)
+ pandas/core/arrays/sparse/dtype.py:416: error: Argument 1 to "find_common_type" has incompatible type "List[Union[Any, dtype[Any], ExtensionDtype]]"; expected "Iterable[Union[dtype[Any], None, Type[Any], _SupportsDType[dtype[Any]], str, Union[Tuple[Any, int], Tuple[Any, Union[SupportsIndex, Sequence[SupportsIndex]]], List[Any], _DTypeDict, Tuple[Any, Any]]]]"  [arg-type]
+ pandas/core/computation/ops.py:86: error: Unused "type: ignore" comment
+ pandas/core/nanops.py:785: error: Incompatible types in assignment (expression has type "Union[Any, float]", variable has type "ndarray[Any, dtype[Any]]")  [assignment]
+ pandas/core/ops/array_ops.py:395: error: Unused "type: ignore" comment
+ pandas/core/arrays/sparse/array.py:470: error: Argument 1 to "na_value_for_dtype" has incompatible type "Union[Any, ExtensionDtype, dtype[Any], Type[object]]"; expected "Union[dtype[Any], ExtensionDtype]"  [arg-type]
+ pandas/plotting/_core.py:964: error: Argument 1 to "len" has incompatible type "Union[Any, bool]"; expected "Sized"  [arg-type]
+ pandas/core/indexes/interval.py:523: error: Unused "type: ignore" comment
+ pandas/core/internals/blocks.py:578: error: Item "ndarray[Any, Any]" of "Union[ndarray[Any, Any], ExtensionArray, Any]" has no attribute "_replace"  [union-attr]
+ pandas/core/internals/blocks.py:578: error: Item "ExtensionArray" of "Union[ndarray[Any, Any], ExtensionArray, Any]" has no attribute "_replace"  [union-attr]
+ pandas/core/internals/blocks.py:731: error: Argument "mask" to "_replace_coerce" of "Block" has incompatible type "Union[ExtensionArray, ndarray[Any, Any], bool]"; expected "ndarray[Any, dtype[bool_]]"  [arg-type]
+ pandas/io/formats/style_render.py:894: error: Unsupported operand types for + ("List[Dict[str, Union[List[Tuple[str, Union[str, int, float]]], Any, str]]]" and "List[Dict[str, List[Tuple[str, Union[str, int, float]]]]]")  [operator]

dragonchain (https://github.com/dragonchain/dragonchain)
+ dragonchain/webserver/lib/verifications.py:85:12: error: Incompatible return value type (got "Optional[Any]", expected "str")
- dragonchain/job_processor/contract_job.py:307:24: error: Cannot call function of unknown type
- dragonchain/job_processor/contract_job.py:344:20: error: Cannot call function of unknown type

Tanjun (https://github.com/FasterSpeeding/Tanjun)
- tanjun/dependencies/owners.py:174: error: Returning Any from function declared to return "bool"  [no-any-return]
- tanjun/dependencies/owners.py:184: error: Returning Any from function declared to return "bool"  [no-any-return]

manticore (https://github.com/trailofbits/manticore)
+ manticore/native/manticore.py:296: error: Incompatible types in assignment (expression has type "Callable[[Any, Any], Any]", variable has type "Union[Callable[[Any, Any, Any], Any], Callable[[Any, Any, Any, Any], Any]]")

@hauntsaninja
Copy link
Collaborator

Fixed in #17427

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.

3 participants