-
Notifications
You must be signed in to change notification settings - Fork 310
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
dlax
wants to merge
21
commits into
urwid:master
Choose a base branch
from
dlax:typing
base: master
Could not load branches
Branch not found: {{ refName }}
Could not load tags
Nothing to show
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
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. |
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.
Yes, I was aware of that. Based on the work I did here, I think it's
probably not worth doing more until py2 support gets dropped.
|
Hi 👋 Python 2.x is no longer supported, so this is probably to be looked at again. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Checklist
master
branchtox
successfully in local environmentDescription
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.