MCPcopy
hub / github.com/python/mypy / append_invariance_notes

Function append_invariance_notes

mypy/messages.py:3399–3428  ·  view source on GitHub ↗

Explain that the type is invariant and give notes for how to solve the issue.

(
    notes: list[str], arg_type: Instance, expected_type: Instance
)

Source from the content-addressed store, hash-verified

3397
3398
3399def append_invariance_notes(
3400 notes: list[str], arg_type: Instance, expected_type: Instance
3401) -> list[str]:
3402 """Explain that the type is invariant and give notes for how to solve the issue."""
3403 invariant_type = ""
3404 covariant_suggestion = ""
3405 if (
3406 arg_type.type.fullname == "builtins.list"
3407 and expected_type.type.fullname == "builtins.list"
3408 and is_subtype(arg_type.args[0], expected_type.args[0])
3409 ):
3410 invariant_type = "list"
3411 covariant_suggestion = 'Consider using "Sequence" instead, which is covariant'
3412 elif (
3413 arg_type.type.fullname == "builtins.dict"
3414 and expected_type.type.fullname == "builtins.dict"
3415 and is_same_type(arg_type.args[0], expected_type.args[0])
3416 and is_subtype(arg_type.args[1], expected_type.args[1])
3417 ):
3418 invariant_type = "dict"
3419 covariant_suggestion = (
3420 'Consider using "Mapping" instead, which is covariant in the value type'
3421 )
3422 if invariant_type and covariant_suggestion:
3423 notes.append(
3424 f'"{invariant_type}" is invariant -- see '
3425 + "https://mypy.readthedocs.io/en/stable/common_issues.html#variance"
3426 )
3427 notes.append(covariant_suggestion)
3428 return notes
3429
3430
3431def append_union_note(

Callers 2

check_subtypeMethod · 0.90
incompatible_argumentMethod · 0.85

Calls 3

is_subtypeFunction · 0.90
is_same_typeFunction · 0.90
appendMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…