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

Allow overriding an attribute with a property (#4125) #11349

Closed

Conversation

tyralla
Copy link
Collaborator

@tyralla tyralla commented Oct 17, 2021

This PR fixes the bug reported here. Note that it does not fix the bugs noted later (e.g. this one) in the same issue. It seems like mypy does not realise the "x = property(getter, setter)" syntax at all (during semantic analysis), or am I missing something?

Also, note that I check for equality rather than a subtype relationship for the attribute and the property's return type. As far as I know, properties still do not allow different setter and getter types. As long as this holds, allowing a subtype relationship would violate LSP.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

add attribute `property_funcdef` to class `Var`.
prepare attribute `property_funcdef` of class `Var` during semantic analysis
adjust checks in module `checker.py`.
adjust checks in module `checkmember`.
@tyralla
Copy link
Collaborator Author

tyralla commented Nov 1, 2021

I added an (very unpolished) approach to handle the "x = property(getter, setter)" syntax. I'm still not sure if it could be the right way to go.

@github-actions

This comment has been minimized.

quick fixes to satisfy mypy_self_check
@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

clearer assertion in `checkmember`.
@github-actions

This comment has been minimized.

@tyralla
Copy link
Collaborator Author

tyralla commented Nov 2, 2021

So, there a no crashes anymore. Next, I need to investigate the "Cannot determine type of " error.

Quick fix for "Cannot determine type of" errors with suitable test extensions. (so far, skipping get_attribute_hook)
@github-actions
Copy link
Contributor

github-actions bot commented Nov 3, 2021

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

optuna (https://github.com/optuna/optuna.git)
+ optuna/study/_multi_objective.py:20: error: Value of type "Optional[List[float]]" is not indexable
+ optuna/study/_multi_objective.py:21: error: Value of type "Optional[List[float]]" is not indexable
+ optuna/storages/_redis.py:519: error: Incompatible types in assignment (expression has type "Sequence[float]", variable has type "Optional[List[float]]")
+ optuna/storages/_in_memory.py:335: error: Incompatible types in assignment (expression has type "Sequence[float]", variable has type "Optional[List[float]]")
+ optuna/storages/_cached_storage.py:299: error: Incompatible types in assignment (expression has type "Sequence[float]", variable has type "Optional[List[float]]")
+ optuna/visualization/_pareto_front.py:137: error: Value of type "Optional[List[float]]" is not indexable
+ optuna/visualization/_pareto_front.py:138: error: Value of type "Optional[List[float]]" is not indexable
+ optuna/visualization/_pareto_front.py:153: error: Value of type "Optional[List[float]]" is not indexable
+ optuna/visualization/_pareto_front.py:154: error: Value of type "Optional[List[float]]" is not indexable
+ optuna/visualization/_pareto_front.py:221: error: Value of type "Optional[List[float]]" is not indexable
+ optuna/visualization/_pareto_front.py:222: error: Value of type "Optional[List[float]]" is not indexable
+ optuna/visualization/_pareto_front.py:223: error: Value of type "Optional[List[float]]" is not indexable
+ optuna/visualization/_pareto_front.py:238: error: Value of type "Optional[List[float]]" is not indexable
+ optuna/visualization/_pareto_front.py:239: error: Value of type "Optional[List[float]]" is not indexable
+ optuna/visualization/_pareto_front.py:240: error: Value of type "Optional[List[float]]" is not indexable
+ optuna/visualization/matplotlib/_pareto_front.py:149: error: Value of type "Optional[List[float]]" is not indexable
+ optuna/visualization/matplotlib/_pareto_front.py:150: error: Value of type "Optional[List[float]]" is not indexable
+ optuna/visualization/matplotlib/_pareto_front.py:156: error: Value of type "Optional[List[float]]" is not indexable
+ optuna/visualization/matplotlib/_pareto_front.py:157: error: Value of type "Optional[List[float]]" is not indexable
+ optuna/visualization/matplotlib/_pareto_front.py:221: error: Value of type "Optional[List[float]]" is not indexable
+ optuna/visualization/matplotlib/_pareto_front.py:222: error: Value of type "Optional[List[float]]" is not indexable
+ optuna/visualization/matplotlib/_pareto_front.py:223: error: Value of type "Optional[List[float]]" is not indexable
+ optuna/visualization/matplotlib/_pareto_front.py:230: error: Value of type "Optional[List[float]]" is not indexable
+ optuna/visualization/matplotlib/_pareto_front.py:231: error: Value of type "Optional[List[float]]" is not indexable
+ optuna/visualization/matplotlib/_pareto_front.py:232: error: Value of type "Optional[List[float]]" is not indexable
+ optuna/samplers/_nsga2.py:381: error: Argument 1 to "len" has incompatible type "Optional[List[float]]"; expected "Sized"
+ optuna/samplers/_nsga2.py:382: error: Value of type "Optional[List[float]]" is not indexable
+ optuna/samplers/_nsga2.py:384: error: Value of type "Optional[List[float]]" is not indexable
+ optuna/samplers/_nsga2.py:385: error: Value of type "Optional[List[float]]" is not indexable
+ optuna/samplers/_nsga2.py:397: error: Value of type "Optional[List[float]]" is not indexable
+ optuna/samplers/_nsga2.py:398: error: Value of type "Optional[List[float]]" is not indexable
+ optuna/integration/wandb.py:106: error: Argument 1 to "len" has incompatible type "Optional[List[float]]"; expected "Sized"
+ optuna/integration/wandb.py:108: error: Argument 1 to "len" has incompatible type "Optional[List[float]]"; expected "Sized"
+ optuna/integration/wandb.py:114: error: Argument 1 to "len" has incompatible type "Optional[List[float]]"; expected "Sized"
+ optuna/integration/wandb.py:119: error: Argument 1 to "len" has incompatible type "Optional[List[float]]"; expected "Sized"
+ optuna/integration/wandb.py:126: error: Argument 2 to "zip" has incompatible type "Optional[List[float]]"; expected "Iterable[float]"
+ optuna/integration/botorch.py:462: error: Argument 1 to "len" has incompatible type "Optional[List[float]]"; expected "Sized"
+ optuna/integration/botorch.py:464: error: Argument 2 to "zip" has incompatible type "Optional[List[float]]"; expected "Iterable[float]"
+ tutorial/20_recipes/002_multi_objective.py:129: error: Value of type "Optional[List[float]]" is not indexable
+ tests/visualization_tests/test_contour.py:36: error: Value of type "Optional[List[float]]" is not indexable
+ tests/visualization_tests/test_contour.py:40: error: Value of type "Optional[List[float]]" is not indexable
+ tests/trial_tests/test_frozen.py:402: error: Incompatible types in assignment (expression has type "Tuple[float, float]", variable has type "Optional[List[float]]")
+ tests/study_tests/test_study.py:949: error: Argument 1 to "tuple" has incompatible type "Optional[List[float]]"; expected "Iterable[float]"
+ tests/storages_tests/rdb_tests/test_storage.py:269: error: Value of type "Optional[List[float]]" is not indexable
+ tests/storages_tests/rdb_tests/test_storage.py:270: error: Value of type "Optional[List[float]]" is not indexable

core (https://github.com/home-assistant/core.git)
+ homeassistant/components/recorder/models.py:489: error: unused "type: ignore" comment
+ homeassistant/components/recorder/models.py:506: error: unused "type: ignore" comment
+ homeassistant/components/recorder/models.py:518: error: unused "type: ignore" comment
+ homeassistant/components/recorder/models.py:530: error: unused "type: ignore" comment

werkzeug (https://github.com/pallets/werkzeug.git)
+ src/werkzeug/exceptions.py:266: error: unused "type: ignore" comment
- tests/test_wrappers.py:237: error: Function is missing a return type annotation
- tests/test_wrappers.py:282: note: (Skipping most remaining errors due to unresolved imports or missing stubs; fix these first)
+ tests/test_wrappers.py:237: note: (Skipping most remaining errors due to unresolved imports or missing stubs; fix these first)

sympy (https://github.com/sympy/sympy.git)
+ sympy/stats/rv.py:336: error: Overriding an attribute with a property requires defining a setter method
+ sympy/stats/rv.py:337: error: Overriding an attribute with a property requires defining a setter method
+ sympy/stats/rv.py:367: error: Overriding an attribute with a property requires defining a setter method
+ sympy/stats/rv.py:368: error: Overriding an attribute with a property requires defining a setter method

pandas (https://github.com/pandas-dev/pandas.git)
+ pandas/core/indexes/base.py:1618: error: Argument 1 to "len" has incompatible type "Optional[Any]"; expected "Sized"  [arg-type]
+ pandas/core/indexes/base.py:1618: error: Argument 1 to "len" has incompatible type "None"; expected "Sized"  [arg-type]
+ pandas/core/indexes/base.py:1620: error: Argument 1 to "len" has incompatible type "None"; expected "Sized"  [arg-type]
+ pandas/core/indexes/base.py:1620: error: Argument 1 to "len" has incompatible type "Optional[Any]"; expected "Sized"  [arg-type]
+ pandas/core/indexes/base.py:1624: error: List or tuple expected as variable arguments  [misc]
+ pandas/core/indexes/base.py:1626: error: Incompatible return value type (got "Optional[Any]", expected "List[Hashable]")  [return-value]
+ pandas/core/indexes/base.py:4354: error: List or tuple expected as variable arguments  [misc]
+ pandas/core/indexes/base.py:4355: error: List or tuple expected as variable arguments  [misc]
+ pandas/core/indexes/base.py:4415: error: "None" has no attribute "index"  [attr-defined]
+ pandas/core/indexes/base.py:7044: error: Need type annotation for "name_tups"  [var-annotated]
+ pandas/core/indexes/base.py:7044: error: Argument 1 to "tuple" has incompatible type "None"; expected "Iterable[<nothing>]"  [arg-type]
- pandas/core/arrays/datetimes.py:483: error: Incompatible types in assignment (expression has type "Union[Union[Period, Timestamp, Timedelta], NaTType]", variable has type "DatetimeArray")  [assignment]
- pandas/core/arrays/datetimes.py:487: error: Incompatible types in assignment (expression has type "Union[Union[Period, Timestamp, Timedelta], NaTType]", variable has type "DatetimeArray")  [assignment]
- pandas/core/arrays/datetimes.py:489: error: Incompatible types in assignment (expression has type "Union[Union[Period, Timestamp, Timedelta], NaTType]", variable has type "DatetimeArray")  [assignment]
+ pandas/core/apply.py:207: error: Item "IndexOpsMixin" of "Union[_T, DataFrame, Any]" has no attribute "transform"  [union-attr]
+ pandas/core/apply.py:752: error: Incompatible types in assignment (expression has type "Union[DataFrame, _T, None]", variable has type "Union[DataFrame, Series, None]")  [assignment]
+ pandas/io/pytables.py:3372: error: Need type annotation for "l"  [var-annotated]
+ pandas/io/pytables.py:3373: error: Argument 1 to "enumerate" has incompatible type "None"; expected "Iterable[<nothing>]"  [arg-type]
+ pandas/io/pytables.py:3868: error: No overload variant of "list" matches argument type "None"  [call-overload]
+ pandas/io/pytables.py:3868: note: Possible overload variants:
+ pandas/io/pytables.py:3868: note:     def [_T] __init__(self) -> List[_T]
+ pandas/io/pytables.py:3868: note:     def [_T] __init__(self, iterable: Iterable[_T]) -> List[_T]
+ pandas/io/parquet.py:133: error: "None" has no attribute "__iter__" (not iterable)  [attr-defined]
- pandas/io/parsers/python_parser.py:984: error: ndarray_obj_2d? has no attribute "T"  [attr-defined]
+ pandas/core/series.py:304: error: unused "type: ignore" comment
+ pandas/core/generic.py:1336: error: "None" has no attribute "__iter__" (not iterable)  [attr-defined]
+ pandas/core/generic.py:1782: error: Unsupported right operand type for in ("None")  [operator]
+ pandas/core/frame.py:1868: error: No overload variant of "list" matches argument type "None"  [call-overload]
+ pandas/core/frame.py:1868: note: Possible overload variants:
+ pandas/core/frame.py:1868: note:     def [_T] __init__(self) -> List[_T]
+ pandas/core/frame.py:1868: note:     def [_T] __init__(self, iterable: Iterable[_T]) -> List[_T]
+ pandas/core/frame.py:1869: note: (Skipping most remaining errors due to unresolved imports or missing stubs; fix these first)
- pandas/core/window/rolling.py:1117: error: "None" not callable  [misc]
- pandas/core/internals/managers.py:169: error: "BaseBlockManager" has no attribute "_rebuild_blknos_and_blklocs"  [attr-defined]
- pandas/core/internals/managers.py:180: error: "BaseBlockManager" has no attribute "_rebuild_blknos_and_blklocs"  [attr-defined]
- pandas/core/internals/array_manager.py:308: error: Item "ExtensionArray" of "Union[Any, ExtensionArray]" has no attribute "_data"  [union-attr]
- pandas/compat/pickle_compat.py:272: error: "Unpickler" has no attribute "is_verbose"  [attr-defined]
- pandas/core/groupby/ops.py:282: error: Overloaded function signature 2 will never be matched: signature 1's parameter type(s) are the same or broader  [misc]
- pandas/tests/resample/test_datetime_index.py:1775: note: (Skipping most remaining errors due to unresolved imports or missing stubs; fix these first)

porcupine (https://github.com/Akuli/porcupine.git)
+ porcupine/plugins/pastebin.py:104: error: unused "type: ignore" comment
+ porcupine/plugins/pastebin.py:105: error: unused "type: ignore" comment

bidict (https://github.com/jab/bidict.git)
+ bidict/_orderedbase.py:193: error: Incompatible types in assignment (expression has type "_Node", variable has type "_SentinelNode")  [assignment]
+ bidict/_orderedbase.py:211: error: Item "None" of "Optional[_Node]" has no attribute "nxt"  [union-attr]
+ bidict/_orderedbase.py:212: error: Item "None" of "Optional[_Node]" has no attribute "prv"  [union-attr]
+ bidict/_orderedbase.py:230: error: Item "None" of "Optional[_Node]" has no attribute "nxt"  [union-attr]
+ bidict/_orderedbidict.py:77: error: Item "None" of "Optional[_Node]" has no attribute "nxt"  [union-attr]
+ bidict/_orderedbidict.py:78: error: Item "None" of "Optional[_Node]" has no attribute "prv"  [union-attr]
+ bidict/_orderedbidict.py:84: error: Item "None" of "Optional[_Node]" has no attribute "nxt"  [union-attr]
+ bidict/_orderedbidict.py:89: error: Item "None" of "Optional[_Node]" has no attribute "prv"  [union-attr]

@tyralla
Copy link
Collaborator Author

tyralla commented Nov 3, 2021

The "Cannot determine type of " problem is actually fixed. Next steps:

  1. Check the details of the diff from the mypy primer.
  2. Support keyword arguments (e.g. x = "property(fset=setter, fget=getter").
  3. What about overriding properties with attributes?
  4. Refactor everything.

@tyralla
Copy link
Collaborator Author

tyralla commented Nov 3, 2021

Alternative approach: python/typeshed#5987

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.

1 participant