Skip to content

Commit 8ab39f9

Browse files
committed
Upgrade to mypy v0.501 --strict
1 parent 1e05b44 commit 8ab39f9

File tree

11 files changed

+154
-129
lines changed

11 files changed

+154
-129
lines changed

Makefile

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -173,9 +173,7 @@ mypy: ${PYSOURCES}
173173
rm -Rf typeshed/2.7/ruamel/yaml
174174
ln -s $(shell python -c 'from __future__ import print_function; import ruamel.yaml; import os.path; print(os.path.dirname(ruamel.yaml.__file__))') \
175175
typeshed/2.7/ruamel/
176-
MYPYPATH=typeshed/2.7 mypy --py2 --disallow-untyped-calls \
177-
--fast-parser --warn-redundant-casts --warn-unused-ignores \
178-
schema_salad
176+
MYPYPATH=typeshed/2.7 mypy --py2 --strict schema_salad/*.py
179177

180178
jenkins:
181179
rm -Rf env && virtualenv env

schema_salad/jsonld_context.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,35 +19,36 @@
1919
import urlparse
2020
import logging
2121
from .aslist import aslist
22-
from typing import Any, cast, Dict, Iterable, Tuple, Union
22+
from typing import (cast, Any, Dict, Iterable, List, Optional, Text, Tuple,
23+
Union)
2324
from .ref_resolver import Loader, ContextType
2425

2526
_logger = logging.getLogger("salad")
2627

2728

2829
def pred(datatype, # type: Dict[str, Union[Dict, str]]
29-
field, # type: Dict
30+
field, # type: Optional[Dict]
3031
name, # type: str
3132
context, # type: ContextType
3233
defaultBase, # type: str
3334
namespaces # type: Dict[str, rdflib.namespace.Namespace]
3435
):
35-
# type: (...) -> Union[Dict, str]
36+
# type: (...) -> Union[Dict, Text]
3637
split = urlparse.urlsplit(name)
3738

38-
vee = None # type: Union[str, unicode]
39+
vee = None # type: Optional[Union[str, unicode]]
3940

40-
if split.scheme:
41+
if split.scheme != '':
4142
vee = name
4243
(ns, ln) = rdflib.namespace.split_uri(unicode(vee))
4344
name = ln
4445
if ns[0:-1] in namespaces:
4546
vee = unicode(namespaces[ns[0:-1]][ln])
4647
_logger.debug("name, v %s %s", name, vee)
4748

48-
v = None # type: Any
49+
v = None # type: Optional[Dict]
4950

50-
if field and "jsonldPredicate" in field:
51+
if field is not None and "jsonldPredicate" in field:
5152
if isinstance(field["jsonldPredicate"], dict):
5253
v = {}
5354
for k, val in field["jsonldPredicate"].items():
@@ -132,14 +133,15 @@ def process_type(t, # type: Dict[str, Any]
132133

133134
_logger.debug("Processing field %s", i)
134135

135-
v = pred(t, i, fieldname, context, defaultPrefix, namespaces)
136+
v = pred(t, i, fieldname, context, defaultPrefix,
137+
namespaces) # type: Union[Dict[Any, Any], unicode, None]
136138

137139
if isinstance(v, basestring):
138140
v = v if v[0] != "@" else None
139-
else:
141+
elif v is not None:
140142
v = v["_@id"] if v.get("_@id", "@")[0] != "@" else None
141143

142-
if v:
144+
if bool(v):
143145
(ns, ln) = rdflib.namespace.split_uri(unicode(v))
144146
if ns[0:-1] in namespaces:
145147
propnode = namespaces[ns[0:-1]][ln]

schema_salad/main.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,11 @@ def main(argsl=None): # type: (List[str]) -> int
146146
metactx = schema_raw_doc.get("$namespaces", {})
147147
if "$base" in schema_raw_doc:
148148
metactx["@base"] = schema_raw_doc["$base"]
149-
(schema_ctx, rdfs) = jsonld_context.salad_to_jsonld_context(schema_doc, metactx)
149+
if schema_doc is not None:
150+
(schema_ctx, rdfs) = jsonld_context.salad_to_jsonld_context(
151+
schema_doc, metactx)
152+
else:
153+
raise Exception("schema_doc is None??")
150154

151155
# Create the loader that will be used to load the target document.
152156
document_loader = Loader(schema_ctx)

schema_salad/makedoc.py

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from .add_dictlist import add_dictlist
1313
import re
1414
import argparse
15-
from typing import Any, IO, Union
15+
from typing import cast, Any, Dict, IO, List, Optional, Set, Text, Union
1616

1717
_logger = logging.getLogger("salad")
1818

@@ -35,7 +35,7 @@ def has_types(items): # type: (Any) -> List[basestring]
3535
return []
3636

3737

38-
def linkto(item):
38+
def linkto(item): # type: (Text) -> Text
3939
_, frg = urlparse.urldefrag(item)
4040
return "[%s](#%s)" % (frg, to_id(frg))
4141

@@ -46,10 +46,10 @@ def __init__(self): # type: () -> None
4646
super(mistune.Renderer, self).__init__()
4747
self.options = {}
4848

49-
def header(self, text, level, raw=None):
49+
def header(self, text, level, raw=None): # type: (Text, int, Any) -> Text
5050
return """<h%i id="%s">%s</h%i>""" % (level, to_id(text), text, level)
5151

52-
def table(self, header, body):
52+
def table(self, header, body): # type: (Text, Text) -> Text
5353
return (
5454
'<table class="table table-striped">\n<thead>%s</thead>\n'
5555
'<tbody>\n%s</tbody>\n</table>\n'
@@ -136,7 +136,7 @@ def number_headings(toc, maindoc): # type: (ToC, str) -> str
136136

137137
if not skip:
138138
m = re.match(r'^(#+) (.*)', line)
139-
if m:
139+
if m is not None:
140140
num = toc.add_entry(len(m.group(1)), m.group(2))
141141
line = "%s %s %s" % (m.group(1), num, m.group(2))
142142
line = re.sub(r'^(https?://\S+)', r'[\1](\1)', line)
@@ -167,7 +167,7 @@ def __init__(self, toc, j, renderlist, redirects):
167167
self.docAfter = {} # type: Dict[str, List]
168168
self.rendered = set() # type: Set[str]
169169
self.redirects = redirects
170-
self.title = None # type: str
170+
self.title = None # type: Optional[str]
171171

172172
for t in j:
173173
if "extends" in t:
@@ -224,7 +224,7 @@ def typefmt(self,
224224
tp, # type: Any
225225
redirects, # type: Dict[str, str]
226226
nbsp=False, # type: bool
227-
jsonldPredicate=None # type: Dict[str, str]
227+
jsonldPredicate=None # type: Optional[Dict[str, str]]
228228
):
229229
# type: (...) -> Union[str, unicode]
230230
global primitiveType
@@ -237,7 +237,7 @@ def typefmt(self,
237237
if tp["type"] == "https://w3id.org/cwl/salad#array":
238238
ar = "array&lt;%s&gt;" % (self.typefmt(
239239
tp["items"], redirects, nbsp=True))
240-
if jsonldPredicate and "mapSubject" in jsonldPredicate:
240+
if jsonldPredicate is not None and "mapSubject" in jsonldPredicate:
241241
if "mapPredicate" in jsonldPredicate:
242242
ar += " | map&lt;%s.%s,&nbsp;%s.%s&gt" % (self.typefmt(tp["items"], redirects),
243243
jsonldPredicate[
@@ -251,7 +251,7 @@ def typefmt(self,
251251
self.typefmt(tp["items"], redirects))
252252
return ar
253253
if tp["type"] in ("https://w3id.org/cwl/salad#record", "https://w3id.org/cwl/salad#enum"):
254-
frg = schema.avro_name(tp["name"])
254+
frg = cast(Text, schema.avro_name(tp["name"]))
255255
if tp["name"] in redirects:
256256
return """<a href="%s">%s</a>""" % (redirects[tp["name"]], frg)
257257
elif tp["name"] in self.typemap:
@@ -267,9 +267,10 @@ def typefmt(self,
267267
return """<a href="%s">%s</a>""" % (primitiveType, schema.avro_name(str(tp)))
268268
else:
269269
_, frg = urlparse.urldefrag(tp)
270-
if frg:
270+
if frg is not '':
271271
tp = frg
272272
return """<a href="#%s">%s</a>""" % (to_id(tp), tp)
273+
raise Exception("We should not be here!")
273274

274275
def render_type(self, f, depth): # type: (Dict[str, Any], int) -> None
275276
if f["name"] in self.rendered or f["name"] in self.redirects:
@@ -328,9 +329,11 @@ def extendsfrom(item, ex):
328329
doc = ""
329330

330331
if self.title is None and f["doc"]:
331-
self.title = f["doc"][0:f["doc"].index("\n")]
332-
if self.title.startswith('# '):
333-
self.title = self.title[2:]
332+
title = f["doc"][0:f["doc"].index("\n")]
333+
if title.startswith('# '):
334+
self.title = title[2:]
335+
else:
336+
self.title = title
334337

335338
if f["type"] == "documentation":
336339
f["doc"] = number_headings(self.toc, f["doc"])

0 commit comments

Comments
 (0)