Skip to content
This repository has been archived by the owner on Mar 8, 2024. It is now read-only.

Commit

Permalink
adding support for union/list/dict/etc. types
Browse files Browse the repository at this point in the history
  • Loading branch information
jpetrucciani committed Sep 9, 2019
1 parent efe9bbd commit e6d7dc7
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 9 deletions.
69 changes: 68 additions & 1 deletion Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

51 changes: 43 additions & 8 deletions archives.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""
archives
- perhaps the archives are incomplete?
perhaps the archives are incomplete?
"""
import click
import os
Expand All @@ -13,7 +13,7 @@
from typing import Callable, Iterator, Iterable, List, Pattern, Set, Tuple, Union


__version__ = "0.1"
__version__ = "0.2"
DEFAULT_EXCLUDES_LIST = [
r"\.eggs",
r"\.git",
Expand Down Expand Up @@ -106,6 +106,32 @@ def no_desc(obj: Union["Class", "Function", "Module"]) -> bool:
]


class Annotation:
"""representation of a type annotation in python code"""

def __init__(self, anno) -> None:
"""annotation constructor"""
self.type = ""
self._annotation = anno
if isinstance(anno, ast3.Name):
self.type = anno.id
if isinstance(anno, ast3.Subscript):
value = anno.slice.value # type: ignore
if isinstance(value, ast3.Name):
internal = value.id
else:
internal = ", ".join([x.s for x in value.elts])
self.type = f"{anno.value.id}[{internal}]"

def __str__(self) -> str:
"""string representation"""
return self.type

def __repr__(self) -> str:
"""repr representation"""
return self.__str__()


class Doc:
"""representation of a doc string"""

Expand Down Expand Up @@ -146,10 +172,11 @@ def __init__(self, arg: ast3.arg) -> None:
self.column = arg.col_offset
self.name = arg.arg
if arg.annotation:
anno = arg.annotation
self.typed = True
self.type = arg.annotation.id # type: ignore
self.type_line = arg.annotation.lineno
self.type_column = arg.annotation.col_offset
self.type = Annotation(anno)
self.type_line = anno.lineno
self.type_column = anno.col_offset

def __repr__(self) -> str:
"""repr for arg"""
Expand All @@ -161,6 +188,7 @@ class Function:

def __init__(self, function: ast3.FunctionDef, module: "Module") -> None:
"""easier to use version of the ast function def"""
self._function = function
self.name = function.name
self.line = function.lineno
self.column = function.col_offset
Expand All @@ -181,6 +209,7 @@ def __init__(self, function: ast3.FunctionDef, module: "Module") -> None:
x for x in self.args if not x.typed and x not in DEFAULT_ARG_IGNORE
]
self.doc = None
self.returns = None
self.missing_args: Set[str] = set()
self.unexpected_args: Set[str] = set()
arg_names = set(x.name for x in self.args if x.name not in DEFAULT_ARG_IGNORE)
Expand All @@ -190,6 +219,12 @@ def __init__(self, function: ast3.FunctionDef, module: "Module") -> None:
doc_arg_names = set(x for x, y in self.doc.args.items())
self.missing_args = arg_names - doc_arg_names
self.unexpected_args = doc_arg_names - arg_names
if function.returns:
ret = function.returns
try:
self.returns = ret # type: ignore
except AttributeError:
self.type = ret.value # type: ignore

def __repr__(self) -> str:
"""repr for function"""
Expand Down Expand Up @@ -292,18 +327,18 @@ def get_python_files(


@lru_cache()
def find_project_root(srcs: Iterable[str]) -> Path:
def find_project_root(sources: Iterable[str]) -> Path:
"""
Return a directory containing .git, .hg, or pyproject.toml.
That directory can be one of the directories passed in `srcs` or their
common parent.
If no directory in the tree contains a marker that would specify it's the
project root, the root of the file system is returned.
"""
if not srcs:
if not sources:
return Path("/").resolve()

common_base = min(Path(src).resolve() for src in srcs)
common_base = min(Path(src).resolve() for src in sources)
if common_base.is_dir():
# Append a fake file so `parents` below returns `common_base_dir`, too.
common_base /= "fake-file"
Expand Down

0 comments on commit e6d7dc7

Please sign in to comment.