Code Monkey home page Code Monkey logo

velin's Introduction

Vélin

French for Vellum

Vellum is prepared animal skin or "membrane", typically used as a material for writing on. Parchment is another term for this material, and if vellum is distinguished from this, it is by vellum being made from calfskin, as opposed to that from other animals,[1] or otherwise being of higher quality

install

You may need to get a modified version of numpydoc depending on the stage of development.

$ git clone https://github.com/Carreau/velin
$ cd velin
$ pip install -e . 

(You will need a quite recent pip and flit to do so)

Autoreformat docstrings

This assume your docstrings are in RST/Numpydoc format, and will try to fix common formatting mistakes and typo.

velin [--write] <path-to-file.py> or <path-to-dir>

Without --write vélin will print the suggested diff, with --write it will attempt to update the files.

options

(likely not up to date, make sure to run velin --help to check for new,changed or removed options)

$ velin --help
usage: velin [-h] [--context context] [--unsafe] [--check] [--no-diff] [--black] [--with-placeholder] [--no-color] [--compact] [--no-fail]
             [--space-in-see-also-title] [--space-in-notes-title] [--no-fixers] [--write]
             path [path ...]

reformat the docstrigns of some file

positional arguments:
  path                  Files or folder to reformat

optional arguments:
  -h, --help            show this help message and exit
  --context context     Number of context lines in the diff
  --unsafe              Lift some safety feature (don't fail if updating the docstring is not indempotent
  --check               Print the list of files/lines number and exit with a non-0 exit status, Use it for CI.
  --no-diff             Do not print the diff
  --black               Do not run black on examples
  --with-placeholder    insert missing sections/parameters placehoders
  --no-color
  --compact             Please ignore
  --no-fail
  --space-in-see-also-title
  --space-in-notes-title
  --no-fixers           try to only reformat and does not run fixers heuristics
  --write               Try to write the updated docstring to the files

--no-fixers

Beyond reformatting, vélin will by default try to run a number of heuristics to update your docstrings:

  • Remove non existing but documented parameters,
  • Rename parameter with typos,
  • insert space before colon when necessary.

Unfortunately sometime those heuristics can remove actual content, for example in the malformed DocString below, the Return section would be removed

def sum(a, b):
    """
    Parameters
    ----------
    a : int
        a number
    b : int
        another number
    Returns
    -------
    s :
        sum of a and b

    See Also
    --------
    substract
    """
    return a + b

As there is a missing blank line before return Numpydoc will parse this a 5 parameters, a, b, Returns, ------- and s. As only a and b are in the signature, it will remove the other.

While in this case it will try not to do that because we detect that ------ is likely an underline, there are other case where it's unclear what to do.

You can thus disable those fixers by passing the option --no-fixers

setup.cfg

Ignore files with ignore_patterns, filename or filename:qualified_name. You can (try to), put patterns in there, but it's not guarantied to work yet.

[velin]
ignore_patterns =
   path/to/a.py:Class.func
   path/to/b.py:Class.func

kind of things it fixes

  • Spacing around colon,
  • If one parameter has typo wrt function signature: fix it.
  • Insert all missing parameters with placeholders.

velin's People

Contributors

carreau avatar ianhi avatar joehcq1 avatar keewis avatar mathdugre avatar tacaswell avatar tonyfast avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

velin's Issues

see also: conversion of multi-line blocks to single lines

At the moment, velin will convert this:

See Also
--------
:ref:`some-doc-page`
    User guide page on a related topic.

which is used to link to prose documentation while still being somewhat legible when looking at the code, to:

See Also
--------
:ref:`some-doc-page` : User guide page on a related topic.

Colon aside, I tend to find the first version easier to read, even if the description does not span multiple lines or exceeds the maximum line length. Would it be possible to have a option to avoid this conversion?

Possible edge case

I'm not sure what I'd expect velin to do here but just pointing out that it doesn't currently recognize the returns as a section in this docstring, instead treating it as another parameter. I suppose there is a fundamental ambiguity here unfortunately, so it may not be fixable, but perhaps should be flagged?

def f(blarg):
    """
    Parameters
    ----------
    blarg : bool
        some description
    returns
    -------
    init_val, control
    """
    pass

gives this output:

(mpl-inter) ian@fermented-beans:~/Documents/jupyter/mpl-interactions$ velin v_test.py 
~/mambaforge/envs/mpl-inter/lib/python3.9/site-packages/velin/ref.py:513 in  | v_test.py:f
~/mambaforge/envs/mpl-inter/lib/python3.9/site-packages/velin/ref.py:514 in  |   missing: set()
~/mambaforge/envs/mpl-inter/lib/python3.9/site-packages/velin/ref.py:515 in  |   extra: {'-------', 'returns', 'init_val', 'control'}
~/mambaforge/envs/mpl-inter/lib/python3.9/site-packages/velin/ref.py:513 in  | v_test.py:f
~/mambaforge/envs/mpl-inter/lib/python3.9/site-packages/velin/ref.py:514 in  |   missing: set()
~/mambaforge/envs/mpl-inter/lib/python3.9/site-packages/velin/ref.py:515 in  |   extra: {'-------', 'returns', 'init_val', 'control'}

Bug: velin either does not show a warning if a directory not found, or does nothing

I am trying to run velin src --check --verbose or velin src/ --check --verbose and it says nothing. I do not know if it can not find the folder or the package does not work at all.

The only working command is velin -h. For other commands it is silent and does not show any information at all. I would like at least to know if the folder path was found.

Nicer formatting for missing arguments

This is an awesome project :)

I just started applying it (mpl-extensions/mpl-interactions#168) after I saw your PR to napari. An overall the experience has been great. One potential improvement would be to make the formatting of the warning for missing parameters nicer:
currently it looks like this:
image

And the majority of space is devoted to ~/mambaforge/envs/mpl-inter/lib/python3.9/site-packages/velin/ref.py:513 in | which isn't super important information for fixing my missing arguments (of which there are shockingly many 😢 edit: turns out in part because I document several kwargs that are passed through to matplotlib. Though that's probably not very numpydocy... ) It would be awesome if this instead had a hierarchical organization. Something like:

filename
   Function 1
       missing

   Function 2 
      missing
      extra

velin . --check throws an undefined variable error

This needs_changes is never defined:

velin/velin/ref.py

Lines 760 to 767 in 203568d

f.write(new)
if args.check:
if need_changes:
sys.exit(
"Some files/functions need updates:\n - " + "\n - ".join(need_changes)
)
else:
sys.exit(0)

which causes velin . --check to fail:

Traceback (most recent call last):
  File "/home/ian/mambaforge/envs/micro/bin/velin", line 8, in <module>
    sys.exit(main())
  File "/home/ian/mambaforge/envs/micro/lib/python3.8/site-packages/velin/ref.py", line 762, in main
    if need_changes:
NameError: name 'need_changes' is not defined

though there is a commented out definition a few lines above

# need_changes = []

Option to disable `extra:` when kwarging

When functions pass-through kwargs as parameters, it's sometimes helpful to include specific parameters of note in the docstring. Velin is catching these as "extra" since they don't match up to the function declaration.

Would it be possible to add an option to disable extra-checking, or make it conditional on whether the function has a **kwargs?

Test on multiple projects.

We are likely to find bugs, and inconsistency of styles.

Until the community converge on a style we should (try to) keep the per-project styles and do minimal disruptions.

Make sure we do properly follow numpydoc.

keep the style for specifying default values consistent

numpydoc describes multiple styles for adding default values to the type spec:

copy : bool, default True
copy : bool, default=True
copy : bool, default: True

(as far as I know, these are not the only ones out there, there's at least bool (default True), too)

It would be nice to have velin make sure these are all in the same style. Not sure if autodetecting is possible / reliable, but a setting (as a template?) might work.

Include a bit more information for "DOCSTRING IS EMPTY !!!"

I had the following message printed with __init__ as the additional info as the only feedback. There are quite a few __info__ methods in an average project...

If possible, I think that one should print the class name if nothing else. Probably one would like to do that for all methods, but at least for __xxx__-methods. Filename and line number may also be beneficial.

urls in the `See Also` section result in a `ParseError`

If I put a url into the See Also section, I get a raw numpydoc.docscrape.ParseError:

def f(a):
    """Test function

    Parameters
    ----------
    a : int
        The first parameter.

    See Also
    --------
    https://numpydoc.readthedocs.io/en/latest/format.html#see-also
    """
    return a
Traceback
something went wrong with test.py:f :


    Test function.

    Parameters
    ----------
    a : int
        The first parameter.

    See Also
    --------
    https://numpydoc.readthedocs.io/en/latest/format.html#see-also
    
Traceback (most recent call last):
  File ".../lib/python3.9/site-packages/velin/ref.py", line 709, in compute_new_doc
    doc = NumpyDocString(dedend_docstring(docstr))
  File ".../lib/python3.9/site-packages/velin/ref.py", line 245, in __init__
    super().__init__(*args, **kwargs)
  File ".../lib/python3.9/site-packages/numpydoc/docscrape.py", line 147, in __init__
    self._parse()
  File ".../lib/python3.9/site-packages/numpydoc/docscrape.py", line 402, in _parse
    self['See Also'] = self._parse_see_also(content)
  File ".../lib/python3.9/site-packages/numpydoc/docscrape.py", line 324, in _parse_see_also
    raise ParseError("%s is not a item name" % line)
numpydoc.docscrape.ParseError: https://numpydoc.readthedocs.io/en/latest/format.html#see-also is not a item name in '\nTest function.\n\nParameters\n----------\na : int\n    The first parameter.\n\nSee Also\n--------\nhttps://numpydoc.readthedocs.io/en/latest/format.html#see-also\n'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File ".../bin/velin", line 8, in <module>
    sys.exit(main())
  File ".../lib/python3.9/site-packages/velin/ref.py", line 1074, in main
    new, _fail_check = _reformat_file(
  File ".../lib/python3.9/site-packages/velin/ref.py", line 842, in _reformat_file
    new_doc, d_, jump_to_loc, _fail_check = compute_new_doc(
  File ".../lib/python3.9/site-packages/velin/ref.py", line 711, in compute_new_doc
    raise type(e)(f"error in {fname}:{func_name}") from e
numpydoc.docscrape.ParseError: error in test.py:f

This is probably not the right way to link to external resources, but even in that case I wouldn't expect to see a stacktrace.

check that reformat and file_update is indempotent.

We want to check that reformat(reformat(docstring)) == reformat(docstring) and reformat(reformat(document)) == reformat(document), and also that ast.parse(document) ~= ast.parse(reformat(document)), that was we are sure we don't break user code.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.