Skip to content

Commit

Permalink
Upgrade to mypy v0.501 --strict
Browse files Browse the repository at this point in the history
  • Loading branch information
mr-c committed Mar 6, 2017
1 parent 1e05b44 commit 8ab39f9
Show file tree
Hide file tree
Showing 11 changed files with 154 additions and 129 deletions.
4 changes: 1 addition & 3 deletions Makefile
Expand Up @@ -173,9 +173,7 @@ mypy: ${PYSOURCES}
rm -Rf typeshed/2.7/ruamel/yaml
ln -s $(shell python -c 'from __future__ import print_function; import ruamel.yaml; import os.path; print(os.path.dirname(ruamel.yaml.__file__))') \
typeshed/2.7/ruamel/
MYPYPATH=typeshed/2.7 mypy --py2 --disallow-untyped-calls \
--fast-parser --warn-redundant-casts --warn-unused-ignores \
schema_salad
MYPYPATH=typeshed/2.7 mypy --py2 --strict schema_salad/*.py

jenkins:
rm -Rf env && virtualenv env
Expand Down
22 changes: 12 additions & 10 deletions schema_salad/jsonld_context.py
Expand Up @@ -19,35 +19,36 @@
import urlparse
import logging
from .aslist import aslist
from typing import Any, cast, Dict, Iterable, Tuple, Union
from typing import (cast, Any, Dict, Iterable, List, Optional, Text, Tuple,
Union)
from .ref_resolver import Loader, ContextType

_logger = logging.getLogger("salad")


def pred(datatype, # type: Dict[str, Union[Dict, str]]
field, # type: Dict
field, # type: Optional[Dict]
name, # type: str
context, # type: ContextType
defaultBase, # type: str
namespaces # type: Dict[str, rdflib.namespace.Namespace]
):
# type: (...) -> Union[Dict, str]
# type: (...) -> Union[Dict, Text]
split = urlparse.urlsplit(name)

vee = None # type: Union[str, unicode]
vee = None # type: Optional[Union[str, unicode]]

if split.scheme:
if split.scheme != '':
vee = name
(ns, ln) = rdflib.namespace.split_uri(unicode(vee))
name = ln
if ns[0:-1] in namespaces:
vee = unicode(namespaces[ns[0:-1]][ln])
_logger.debug("name, v %s %s", name, vee)

v = None # type: Any
v = None # type: Optional[Dict]

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

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

v = pred(t, i, fieldname, context, defaultPrefix, namespaces)
v = pred(t, i, fieldname, context, defaultPrefix,
namespaces) # type: Union[Dict[Any, Any], unicode, None]

if isinstance(v, basestring):
v = v if v[0] != "@" else None
else:
elif v is not None:
v = v["_@id"] if v.get("_@id", "@")[0] != "@" else None

if v:
if bool(v):
(ns, ln) = rdflib.namespace.split_uri(unicode(v))
if ns[0:-1] in namespaces:
propnode = namespaces[ns[0:-1]][ln]
Expand Down
6 changes: 5 additions & 1 deletion schema_salad/main.py
Expand Up @@ -146,7 +146,11 @@ def main(argsl=None): # type: (List[str]) -> int
metactx = schema_raw_doc.get("$namespaces", {})
if "$base" in schema_raw_doc:
metactx["@base"] = schema_raw_doc["$base"]
(schema_ctx, rdfs) = jsonld_context.salad_to_jsonld_context(schema_doc, metactx)
if schema_doc is not None:
(schema_ctx, rdfs) = jsonld_context.salad_to_jsonld_context(
schema_doc, metactx)
else:
raise Exception("schema_doc is None??")

# Create the loader that will be used to load the target document.
document_loader = Loader(schema_ctx)
Expand Down
29 changes: 16 additions & 13 deletions schema_salad/makedoc.py
Expand Up @@ -12,7 +12,7 @@
from .add_dictlist import add_dictlist
import re
import argparse
from typing import Any, IO, Union
from typing import cast, Any, Dict, IO, List, Optional, Set, Text, Union

_logger = logging.getLogger("salad")

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


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

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

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

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

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

for t in j:
if "extends" in t:
Expand Down Expand Up @@ -224,7 +224,7 @@ def typefmt(self,
tp, # type: Any
redirects, # type: Dict[str, str]
nbsp=False, # type: bool
jsonldPredicate=None # type: Dict[str, str]
jsonldPredicate=None # type: Optional[Dict[str, str]]
):
# type: (...) -> Union[str, unicode]
global primitiveType
Expand All @@ -237,7 +237,7 @@ def typefmt(self,
if tp["type"] == "https://w3id.org/cwl/salad#array":
ar = "array&lt;%s&gt;" % (self.typefmt(
tp["items"], redirects, nbsp=True))
if jsonldPredicate and "mapSubject" in jsonldPredicate:
if jsonldPredicate is not None and "mapSubject" in jsonldPredicate:
if "mapPredicate" in jsonldPredicate:
ar += " | map&lt;%s.%s,&nbsp;%s.%s&gt" % (self.typefmt(tp["items"], redirects),
jsonldPredicate[
Expand All @@ -251,7 +251,7 @@ def typefmt(self,
self.typefmt(tp["items"], redirects))
return ar
if tp["type"] in ("https://w3id.org/cwl/salad#record", "https://w3id.org/cwl/salad#enum"):
frg = schema.avro_name(tp["name"])
frg = cast(Text, schema.avro_name(tp["name"]))
if tp["name"] in redirects:
return """<a href="%s">%s</a>""" % (redirects[tp["name"]], frg)
elif tp["name"] in self.typemap:
Expand All @@ -267,9 +267,10 @@ def typefmt(self,
return """<a href="%s">%s</a>""" % (primitiveType, schema.avro_name(str(tp)))
else:
_, frg = urlparse.urldefrag(tp)
if frg:
if frg is not '':
tp = frg
return """<a href="#%s">%s</a>""" % (to_id(tp), tp)
raise Exception("We should not be here!")

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

if self.title is None and f["doc"]:
self.title = f["doc"][0:f["doc"].index("\n")]
if self.title.startswith('# '):
self.title = self.title[2:]
title = f["doc"][0:f["doc"].index("\n")]
if title.startswith('# '):
self.title = title[2:]
else:
self.title = title

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

0 comments on commit 8ab39f9

Please sign in to comment.