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

Add minimal typing and mypy configuration #411

Open
wants to merge 21 commits into
base: master
Choose a base branch
from
Open

Conversation

dlax
Copy link
Contributor

@dlax dlax commented Jun 9, 2020

Checklist
  • I've ensured that similar functionality has not already been implemented
  • I've ensured that similar functionality has not earlier been proposed and declined
  • I've branched off the master branch
  • I've merged fresh upstream into my branch recently
  • I've ran tox successfully in local environment
Description

This PR is a first step towards #406. It adds a mypy configuration and contains all needed changes to make mypy happy with the current code base. The changes are as limited as possible : these concern addition of type annotation, refactorings to avoid confusing mypy or ignoring "errors" (most of which being due to the py2 compat).

I tried to make small commits, fixing one thing at a time. The first two commits have been submitted separately in #409.

dlax added 21 commits June 6, 2020 11:35
Support for python 2.4 and 2.5 got dropped in version 1.2.0.
Also, these declarations confuse mypy.
We configure mypy to only scan python files directly in urwid directory,
thus avoiding the tests package (for now) and focus on the non-test code
as a start.

We ignore errors about missing imports for dependencies without stub
files or type declarations.
Instead, we define the '_started' attribute, which is used by 'started'
property in base class.

This fixes the following mypy error:

  urwid/html_fragment.py:47: error: Incompatible types in assignment (expression has type "bool", base class "BaseScreen" defined the type as "property")
As set and documented in parent class Widget.

Fixing a mypy error:

  urwid/wimp.py:612: error: Incompatible types in assignment (expression has type "Set[str]", base class "Widget" defined the type as "FrozenSet[str]")
This fixes the following mypy errors:

    urwid/canvas.py:96: error: Incompatible types in assignment (expression has type "classmethod", variable has type "Callable[[CanvasCache, Any, Any], Any]")
    urwid/canvas.py:118: error: Incompatible types in assignment (expression has type "classmethod", variable has type "Callable[[CanvasCache, Any, Any, Any, Any], Any]")
    urwid/canvas.py:142: error: Incompatible types in assignment (expression has type "classmethod", variable has type "Callable[[CanvasCache, Any], Any]")
    urwid/canvas.py:165: error: Incompatible types in assignment (expression has type "classmethod", variable has type "Callable[[CanvasCache, Any], Any]")
    urwid/canvas.py:174: error: Incompatible types in assignment (expression has type "classmethod", variable has type "Callable[[CanvasCache], Any]")
This is to get this module passed mypy without having to bother about
incompatible types inferred from the py3 section above the py2 one,
until the py2 compat gets dropped.
This seems more correct, and fixes a mypy error:

    urwid/signals.py:54: error: Need type annotation for '__slots__' (hint: "__slots__: List[<type>] = ...")
Mypy complains about "Type[List[Any]]" (i.e. list) not having attribute
"__delslice__" and "__setslice__", which is true on python3. So we just
ignore the error for python2 code path only.
Mypy issues errors like:

    urwid/graphics.py:249: error: Unsupported dynamic base class "with_metaclass"

because it does not recognize urwid's "with_metaclass" definition (only
six's one is supported).

We ignore this error message until py2 support gets dropped.
We extract DelegateToWidgetMixin class and make it used directly instead
of delegate_to_widget_mixin() calls in urwid. Additionally, we define a
DelegateToOriginalWidgetMixin class for common case where the delegated
attribute is "_original_widget".

We do this to help mypy and avoid the following errors:

    urwid/widget.py:1782: error: Unsupported dynamic base class "delegate_to_widget_mixin"
    urwid/decoration.py:89: error: Unsupported dynamic base class "delegate_to_widget_mixin"
    urwid/decoration.py:107: error: Unsupported dynamic base class "delegate_to_widget_mixin"
    urwid/wimp.py:569: error: Unsupported dynamic base class "delegate_to_widget_mixin"
This is needed to avoid mypy's incompatible types assignment:

    urwid/widget.py:1801: error: Incompatible types in assignment (expression has type "str", base class "DelegateToWidgetMixin" defined the type as "None")
This confuses mypy which believes that methods definition in base and
child classes are incompatible:

    urwid/graphics.py:100: error: Definition of "sizing" in base class "WidgetDecoration" is incompatible with definition in base class "DelegateToWidgetMixin"
    urwid/graphics.py:100: error: Definition of "selectable" in base class "WidgetDecoration" is incompatible with definition in base class "DelegateToWidgetMixin"

We thus define plain "def" methods for the proxy class.
Requested by mypy:

    urwid/font.py:84: error: Need type annotation for '_all_fonts' (hint: "_all_fonts: List[<type>] = ...")
As requested by mypy:

    urwid/html_fragment.py:44: error: Need type annotation for 'fragments' (hint: "fragments: List[<type>] = ...")
    urwid/html_fragment.py:45: error: Need type annotation for 'sizes' (hint: "sizes: List[<type>] = ...")
    urwid/html_fragment.py:46: error: Need type annotation for 'keys' (hint: "keys: List[<type>] = ...")
This is useful because mypy fails to infer the extension module.
Silence the following error:

    urwid/escape.py:34: error: Name 'str_util' already defined (by an import)
This is to silence mypy error:

    urwid/monitored_list.py:349: error: Signatures of "__imul__" and "__mul__" are incompatible
@coveralls
Copy link

Coverage Status

Coverage decreased (-0.009%) to 77.879% when pulling 497ef45 on dlax:typing into 2cc5489 on urwid:master.

@ulidtko
Copy link
Collaborator

ulidtko commented Jun 11, 2020

Excellent work!

I didn't review all of it yet (will take some time) — just wanted to let you know: we're on our way to abandon Py2 support altogether (#394), if that helps.

@dlax
Copy link
Contributor Author

dlax commented Jun 11, 2020 via email

@darraghenright
Copy link

Hi 👋 Python 2.x is no longer supported, so this is probably to be looked at again.

@penguinolog penguinolog added Feature Feature request/implementation Waiting for response labels Sep 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Feature Feature request/implementation Waiting for response
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants