Skip to content
/ apimd Public

A Python API compiler for universal Markdown syntax.

License

Notifications You must be signed in to change notification settings

KmolYuan/apimd

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Build status PyPI

apimd

A Python API compiler for universal Markdown syntax.

Required Python 3.9 and above. (for ast.unparse function)

This parser using ast standard library to extract the type annotations (without inference) and docstrings, similar to MyPy. The target modules must be from at least Python 3.0, which is the lowest version with ast support.

A self-compiled example is presented at https://github.com/KmolYuan/apimd/blob/master/docs/apimd-api.md.

Install

Install by pip:

pip install apimd

From Git repository:

pip install .

Command Line Interface

Following syntax are allowed:

apimd module_name
apimd Module-Name=module_name
apimd "Module Name=module_name"

The first is the readable name of the package, and the second is the name used in import syntax.

The output path can be chosen by -d or --dir option, default is docs. Multiple modules are supported either.

apimd module1 module2 -d out_path

If you just want to show output, use dry run mode.

apimd module --dry

Rules

Basically, this compiler can extract docstrings and annotations from those "public" names: (PEP 484, 526)

  • Modules
  • Functions & Generators (support async version)
  • Classes and its methods

According to PEP 8, "public" means a name can't start with underscore symbol _, except magic methods. If the public name (other than the magic name) has no docstring, the compiler will issue a warning. (Naming Conventions)

The names must be defined within the scope of module and class, and supports the use of if and try statements.

Constants

Constants (upper snake case) are no docstring their owned but still listed in module section. Please mention them in the module docstring.

Constant Type Inference

Constant type inference applies to built-in types and containers with built-in types, such as None, int, bool, str, tuple, dict[int, str], etc. You can also annotate them manually.

This function also works in class attributes, but doesn't support unpacking.

Import Inference

A module should list the objects __all__ to prevent other public style names. In this parser, wildcard import syntax (from ... import *) will be ignored, which will cause the name from the statement will lose its parent module. If there has any import statements in the package root __init__.py, the API can be substituted into a short name, for example, change a.b.c to a.c. (Global Variable Names)

Generic Self

To avoid generic self-reference that is not easy to understand, the compiler introduce Self type concept from Rust language, which means the first argument in class should be treated as it and its subclasses.

def method(self, a: int) -> None:
    ...
self a return
Self int None

If a method returns its self, in Python, it can be mark as:

class A:
    _Self = typing.TypeVar('_Self', bound='A')
    def method(self: _Self) -> _Self:
        return self
    @classmethod
    def make_method(cls: typing.Type[_Self]) -> _Self:
        return cls()
self return
Self Self
cls return
type[Self] Self

The example shown at self-compiled documentation.

Improvement from PEPs

In addition to the basic rules, your documentation will be improved for accepted PEPs, even it is only implemented in the future version. (your code still will not be modified)

No. Description
585 Type Hinting Generics In Standard Collections
604 Allow writing union types as X | Y

Section Links

Since the converted title id will remove the period symbol, apimd will insert another HTML anchor id to help you refer to other public names in docstring. The anchor id is generated from the lowercase full name, and replace the periods . by hyphens -. For example, aaa.AAA.bbb_Ccc will become aaa-aaa-bbb_ccc.

Use --no-link to prevent this function.

Generating Table of Contents

Add --toc option to generate the table of contents at the top of the document. This option will force activate section link option.

Stubs

If a module has a stub file (.pyi), the stub file will be loaded for annotations once again. Docstrings should still be written in the module first. For extensions (.so, .pyd or .dylib with Python version suffix), this tool will try to load the docstrings from module if .py file is not found.

About

A Python API compiler for universal Markdown syntax.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages