From f39ec7d2f4b609c0968767590eeb864a48b41401 Mon Sep 17 00:00:00 2001 From: Daniel Schadt Date: Sat, 21 Aug 2021 22:59:52 +0200 Subject: More type fixes This makes mypy happier about how we use .extend, since it doesn't know about the fact that all list items should be either Node or Block, not mixed. In the first case, we can fix it by using .append(current[0]), as mypy will see the isinstance() above and ascribe the correct type. In the second case, we use the cast no-op function to assign the correct type. That should be cheap and makes mypy happy, even if it doesn't give us any runtime checks (which is fine, this is Python after all). In the third case, we're iterating over every element anyway, so we can throw in an assert to make sure the list item has the correct type. This also helps in catching some bugs, in case .convert() returns a mixed list (but only if the first item happens to be a Node). --- wikimini/__init__.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/wikimini/__init__.py b/wikimini/__init__.py index 6c2f2a6..f927a94 100644 --- a/wikimini/__init__.py +++ b/wikimini/__init__.py @@ -5,7 +5,7 @@ import requests from tabulate import tabulate -from typing import List, Union, Tuple, Sequence +from typing import List, Union, Tuple, Sequence, Iterable, cast from .document import ( Plain, BlockLink, InlineLink, Verbatim, Document, Node, Block, ItemList, @@ -81,7 +81,7 @@ class Wikimini: "prop": "revisions", "rvprop": "content", "rvslots": "main", - "rvlimit": 1, + "rvlimit": "1", "titles": title, "format": "json", "formatversion": "2", @@ -142,12 +142,13 @@ class Wikimini: pass # Special case: We're starting a list! elif len(current) == 1 and isinstance(current[0], ItemList): - document.extend(current) + document.append(current[0]) elif isinstance(current[0], Block): - document.extend(current) + document.extend(cast(Iterable[Block], current)) document.append(Paragraph([])) elif isinstance(current[0], Node): for c in current: + assert isinstance(c, Node) insert_into(document, c) return document elif isinstance(obj, mwp.nodes.heading.Heading): -- cgit v1.2.3