Skip to content

Commit 65027e0

Browse files
committed
Placate mypy (redux)
Signed-off-by: Elsie Hupp <github@elsiehupp.com>
1 parent 3a50bb7 commit 65027e0

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+881
-621
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ repos:
99
rev: 1.6.0
1010
hooks:
1111
- id: poetry-check
12-
# - id: poetry-lock
12+
- id: poetry-lock
1313
- id: poetry-export
1414
args: ["-f", "requirements.txt", "-o", "requirements.txt"]
1515
- repo: https://github.com/pre-commit/pre-commit-hooks

poetry.lock

Lines changed: 226 additions & 67 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,18 @@ requests = "^2.31.0"
7777
flake8 = "^3.9.2"
7878
pre-commit = "^2.17.0"
7979
pymarkdown = "^0.1.4"
80+
mypy = "^1.5.1"
81+
types-requests = "^2.31.0.2"
82+
# flake8-black may be unnecessary?
83+
flake8-black = "^0.3.6"
8084

8185
[build-system]
8286
requires = ["poetry-core>=1.0.0"]
8387
build-backend = "poetry.core.masonry.api"
8488

8589
[tool.pymarkdown]
8690
disable-rules = "line-length,no-inline-html"
91+
92+
[tool.mypy]
93+
check_untyped_defs = true
94+
ignore_missing_imports = true

wikiteam3/dumpgenerator/__init__.py

100755100644
Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +0,0 @@
1-
#!/usr/bin/env python3
2-
3-
# DumpGenerator A generator of dumps for wikis
4-
# Copyright (C) 2011-2018 WikiTeam developers
5-
# This program is free software: you can redistribute it and/or modify
6-
# it under the terms of the GNU General Public License as published by
7-
# the Free Software Foundation, either version 3 of the License, or
8-
# (at your option) any later version.
9-
#
10-
# This program is distributed in the hope that it will be useful,
11-
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12-
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13-
# GNU General Public License for more details.
14-
#
15-
# You should have received a copy of the GNU General Public License
16-
# along with this program. If not, see <http://www.gnu.org/licenses/>.
17-
18-
# To learn more, read the documentation:
19-
# https://github.com/WikiTeam/wikiteam/wiki
20-
21-
22-
from wikiteam3.dumpgenerator.dump import DumpGenerator
23-
24-
25-
def main():
26-
DumpGenerator()

wikiteam3/dumpgenerator/__main__.py

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,32 @@
1+
#!/usr/bin/env python3
2+
3+
# DumpGenerator A generator of dumps for wikis
4+
# Copyright (C) 2011-2018 WikiTeam developers
5+
# This program is free software: you can redistribute it and/or modify
6+
# it under the terms of the GNU General Public License as published by
7+
# the Free Software Foundation, either version 3 of the License, or
8+
# (at your option) any later version.
9+
#
10+
# This program is distributed in the hope that it will be useful,
11+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
# GNU General Public License for more details.
14+
#
15+
# You should have received a copy of the GNU General Public License
16+
# along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
18+
# To learn more, read the documentation:
19+
# https://github.com/WikiTeam/wikiteam/wiki
20+
21+
22+
from wikiteam3.dumpgenerator.dump import DumpGenerator
23+
24+
25+
def main():
26+
DumpGenerator()
27+
28+
129
if __name__ == "__main__":
230
import sys
331

4-
from .__init__ import main
5-
632
sys.exit(main())

wikiteam3/dumpgenerator/api/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@
22
from .get_json import getJSON
33
from .handle_status_code import handleStatusCode
44
from .wiki_check import getWikiEngine
5+
6+
__all__ = [checkAPI, checkRetryAPI, mwGetAPIAndIndex, getJSON, handleStatusCode, getWikiEngine] # type: ignore

wikiteam3/dumpgenerator/api/api.py

Lines changed: 36 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import re
2-
import time
3-
from typing import *
4-
from urllib.parse import urljoin, urlparse, urlunparse
2+
from typing import Any, Literal, Optional
3+
from urllib.parse import urljoin, urlparse
54

65
import mwclient
76
import requests
@@ -11,7 +10,8 @@
1110
from .get_json import getJSON
1211

1312

14-
def checkAPI(api="", session: requests.Session = None):
13+
# api="", session: requests.Session = None
14+
def checkAPI(api: str, session: requests.Session):
1515
"""Checking API availability"""
1616
global cj
1717
# handle redirects
@@ -34,29 +34,31 @@ def checkAPI(api="", session: requests.Session = None):
3434
"MediaWiki API URL not found or giving error: HTTP %d" % r.status_code
3535
)
3636
return None
37-
if "MediaWiki API is not enabled for this site." in r.text:
38-
return None
39-
try:
40-
result = getJSON(r)
41-
index = None
42-
if result:
43-
try:
44-
index = (
45-
result["query"]["general"]["server"]
46-
+ result["query"]["general"]["script"]
47-
)
48-
return (True, index, api)
49-
except KeyError:
50-
print("MediaWiki API seems to work but returned no index URL")
51-
return (True, None, api)
52-
except ValueError:
53-
print(repr(r.text))
54-
print("MediaWiki API returned data we could not parse")
55-
return None
37+
if r is not None:
38+
if "MediaWiki API is not enabled for this site." in r.text:
39+
return None
40+
try:
41+
result = getJSON(r)
42+
index = None
43+
if result:
44+
try:
45+
index = (
46+
result["query"]["general"]["server"]
47+
+ result["query"]["general"]["script"]
48+
)
49+
return (True, index, api)
50+
except KeyError:
51+
print("MediaWiki API seems to work but returned no index URL")
52+
return (True, None, api)
53+
except ValueError:
54+
print(repr(r.text))
55+
print("MediaWiki API returned data we could not parse")
56+
return None
5657
return None
5758

5859

59-
def mwGetAPIAndIndex(url="", session: requests.Session = None):
60+
# url=""
61+
def mwGetAPIAndIndex(url: str, session: requests.Session):
6062
"""Returns the MediaWiki API and Index.php"""
6163

6264
api = ""
@@ -108,18 +110,21 @@ def mwGetAPIAndIndex(url="", session: requests.Session = None):
108110
return api, index
109111

110112

111-
def checkRetryAPI(api="", apiclient=False, session: requests.Session = None):
113+
# api="", apiclient=False
114+
def checkRetryAPI(api: str, apiclient: bool, session: requests.Session):
112115
"""Call checkAPI and mwclient if necessary"""
113-
check = None
116+
check: (tuple[Literal[True], Any, str] | tuple[Literal[True], None, str] | None)
114117
try:
115118
check = checkAPI(api, session=session)
116119
except requests.exceptions.ConnectionError as e:
117120
print(f"Connection error: {str(e)}")
121+
check = None
118122

119123
if check and apiclient:
120124
apiurl = urlparse(api)
121125
try:
122-
site = mwclient.Site(
126+
# Returns a value, but we're just checking for an error here
127+
mwclient.Site(
123128
apiurl.netloc,
124129
apiurl.path.replace("api.php", ""),
125130
scheme=apiurl.scheme,
@@ -138,13 +143,14 @@ def checkRetryAPI(api="", apiclient=False, session: requests.Session = None):
138143
)
139144

140145
try:
141-
site = mwclient.Site(
146+
# Returns a value, but we're just checking for an error here
147+
mwclient.Site(
142148
apiurl.netloc,
143149
apiurl.path.replace("api.php", ""),
144150
scheme=newscheme,
145151
pool=session,
146152
)
147153
except KeyError:
148-
check = False
154+
check = False # type: ignore
149155

150-
return check, api
156+
return check, api # type: ignore

wikiteam3/dumpgenerator/api/get_json.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@ def getJSON(request: requests.Response):
88
# request.encoding = request.apparent_encoding
99
try:
1010
return request.json()
11-
except:
11+
except Exception:
1212
# Maybe an older API version which did not return correct JSON
1313
return {}

wikiteam3/dumpgenerator/api/index_check.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@
33
import requests
44

55

6-
def checkIndex(index="", cookies="", session: requests.Session = None):
6+
# index="", cookies="", session=None
7+
def checkIndex(index: str, cookies: str, session: requests.Session):
78
"""Checking index.php availability"""
8-
r = session.post(url=index, data={"title": "Special:Version"}, timeout=30)
9+
r = session.post(url=index, data={"title": "Special:Version"}, timeout=30) # type: ignore
910
if r.status_code >= 400:
1011
print(f"ERROR: The wiki returned status code HTTP {r.status_code}")
1112
return False
Lines changed: 25 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,50 @@
11
import re
22

3+
import requests
4+
35
from wikiteam3.dumpgenerator.api import getJSON
46
from wikiteam3.dumpgenerator.cli import Delay
57
from wikiteam3.dumpgenerator.config import Config
68

79

8-
def getNamespacesScraper(config: Config = None, session=None):
10+
def getNamespacesScraper(config: Config, session: requests.Session):
911
"""Hackishly gets the list of namespaces names and ids from the dropdown in the HTML of Special:AllPages"""
1012
"""Function called if no API is available"""
1113
namespaces = config.namespaces
12-
namespacenames = {0: ""} # main is 0, no prefix
14+
# namespacenames = {0: ""} # main is 0, no prefix
1315
if namespaces:
1416
r = session.post(
15-
url=config.index, params={"title": "Special:Allpages"}, timeout=30
17+
url=config.index, params={"title": "Special:Allpages"}, timeout=30 # type: ignore
1618
)
1719
raw = r.text
18-
Delay(config=config, session=session)
20+
Delay(config=config)
1921

2022
# [^>]*? to include selected="selected"
2123
m = re.compile(
2224
r'<option [^>]*?value=[\'"](?P<namespaceid>\d+)[\'"][^>]*?>(?P<namespacename>[^<]+)</option>'
2325
).finditer(raw)
2426
if "all" in namespaces:
25-
namespaces = []
26-
for i in m:
27-
namespaces.append(int(i.group("namespaceid")))
28-
namespacenames[int(i.group("namespaceid"))] = i.group("namespacename")
27+
namespaces = [int(i.group("namespaceid")) for i in m]
28+
# namespacenames[int(i.group("namespaceid"))] = i.group("namespacename")
2929
else:
30-
# check if those namespaces really exist in this wiki
31-
namespaces2 = []
32-
for i in m:
33-
if int(i.group("namespaceid")) in namespaces:
34-
namespaces2.append(int(i.group("namespaceid")))
35-
namespacenames[int(i.group("namespaceid"))] = i.group(
36-
"namespacename"
37-
)
30+
namespaces2 = [
31+
int(i.group("namespaceid"))
32+
for i in m
33+
if int(i.group("namespaceid")) in namespaces
34+
]
3835
namespaces = namespaces2
3936
else:
4037
namespaces = [0]
4138

4239
namespaces = list(set(namespaces)) # uniques
4340
print("%d namespaces found" % (len(namespaces)))
44-
return namespaces, namespacenames
41+
return namespaces
4542

4643

47-
def getNamespacesAPI(config: Config = None, session=None):
44+
def getNamespacesAPI(config: Config, session: requests.Session):
4845
"""Uses the API to get the list of namespaces names and ids"""
4946
namespaces = config.namespaces
50-
namespacenames = {0: ""} # main is 0, no prefix
47+
# namespacenames = {0: ""} # main is 0, no prefix
5148
if namespaces:
5249
r = session.get(
5350
url=config.api,
@@ -60,37 +57,34 @@ def getNamespacesAPI(config: Config = None, session=None):
6057
timeout=30,
6158
)
6259
result = getJSON(r)
63-
Delay(config=config, session=session)
60+
Delay(config=config)
6461
try:
6562
nsquery = result["query"]["namespaces"]
66-
except KeyError:
63+
except KeyError as ke:
6764
print("Error: could not get namespaces from the API request.")
6865
print("HTTP %d" % r.status_code)
6966
print(r.text)
70-
return None
67+
raise ke
7168

7269
if "all" in namespaces:
73-
namespaces = []
74-
for i in nsquery.keys():
75-
if int(i) < 0: # -1: Special, -2: Media, excluding
76-
continue
77-
namespaces.append(int(i))
78-
namespacenames[int(i)] = nsquery[i]["*"]
70+
namespaces = [int(i) for i in nsquery.keys() if int(i) >= 0]
71+
# -1: Special, -2: Media, excluding
72+
# namespacenames[int(i)] = nsquery[i]["*"]
7973
else:
8074
# check if those namespaces really exist in this wiki
8175
namespaces2 = []
8276
for i in nsquery.keys():
83-
bi = i
77+
# bi = i
8478
i = int(i)
8579
if i < 0: # -1: Special, -2: Media, excluding
8680
continue
8781
if i in namespaces:
8882
namespaces2.append(i)
89-
namespacenames[i] = nsquery[bi]["*"]
83+
# namespacenames[i] = nsquery[bi]["*"]
9084
namespaces = namespaces2
9185
else:
9286
namespaces = [0]
9387

9488
namespaces = list(set(namespaces)) # uniques
9589
print("%d namespaces found" % (len(namespaces)))
96-
return namespaces, namespacenames
90+
return namespaces

0 commit comments

Comments
 (0)