griffe icon indicating copy to clipboard operation
griffe copied to clipboard

Boolean operation in NumPy-style type annotation raises error

Open sisp opened this issue 2 years ago • 4 comments

Describe the bug

A NumPy-style type annotation of the form A or B (e.g. int or float) raises the following error when building MkDocs docs with mkdocstrings:

griffe: Failed to parse annotation from 'BoolOp' node at ...

This issue seems to be related to https://github.com/mkdocstrings/griffe/issues/82 where the comment

it's probably wrong to use boolean operations in annotations (a and b, a or b)

-- https://github.com/mkdocstrings/griffe/issues/82#issuecomment-1166558874

is incorrect according to https://numpydoc.readthedocs.io/en/latest/format.html#parameters.

To Reproduce

"""Docstring with boolean operation in NumPy-style type annotation.

Parameters
----------
value : int or float
    Some int or float value.
"""

Expected behavior This docstring should parse fine.

System:

  • griffe version: 0.22.0
  • Python version: 3.9
  • OS: Linux

sisp avatar Jul 18 '22 15:07 sisp

Parsing the numpy types can be quite hard because they allow free text as well as keywords, here is how we do it in pydoctor: https://github.com/twisted/pydoctor/blob/4176822d7d79e4a55bab6be229266a2c9c85d21b/pydoctor/napoleon/docstring.py#L159 the code is based on sphinx’s napoleon extension.

This code could be adapted to output markdown instead of restructuredtext.

tristanlatr avatar Jul 18 '22 19:07 tristanlatr

/cc @pawamoy

sisp avatar Aug 05 '22 10:08 sisp

I bumped into the same issue ERROR - griffe: Failed to parse annotation from 'BoolOp' node with Google-style docstrings, with argument types (str or None). It is also worth mentioning that my build on readthedocs.org was reported as successful, yet I did not see the expected changes in the result. Only by looking at the raw logs did I notice that griffe reported several errors.

I can work around this issue by using (Optional[str]) instead of str or None

jmp75 avatar Aug 13 '22 01:08 jmp75

The core of the issue is to assume the numpy types as parsable with ast.parse. Because they are not. We must handle them by natural language processing.

Here are a few examples:

List[str] or list(bytes), optional
{"html", "json", "xml"}, optional
list of int or float or None, default: None
complicated string or `strIO <twisted.python.compat.NativeStringIO>`

tristanlatr avatar Aug 13 '22 14:08 tristanlatr

Types that are not valid Python (syntax error) are fine indeed. The issue arises when the type is valid Python but not a valid type. Rather than adding complex logic to parse complex Numpy types, we could consider simply ignoring BoolOp errors (as to not make the build fail). If users want automatic cross-reference, they should use valid types and not natural language. What do you all think?

pawamoy avatar Aug 17 '22 19:08 pawamoy

@pawamoy Would ignoring BoolOp errors suffice for handling some other natural language non-type cases like the following (taken from numpydoc)?

Parameters
----------
dtype : data-type
iterable : iterable object
shape : int or tuple of int
files : list of str

sisp avatar Aug 17 '22 19:08 sisp

  • data-type: not sure, sub node probably not supported, will probably trigger a warning as well
  • iterable object: syntax error, ignored
  • int or tuple of int: syntax error, ignored
  • list of str: syntax error, ignored

pawamoy avatar Aug 17 '22 20:08 pawamoy

I've decided to use the DEBUG level for such log messages when parsing Numpydoc docstrings. This will prevent your builds from failing on errors or warnings. Other parsers are left untouched and will continue logging errors.

pawamoy avatar Sep 23 '22 17:09 pawamoy

Thanks, @pawamoy! :pray: NumPy-style parameter annotations work great now! :slightly_smiling_face:

sisp avatar Oct 05 '22 15:10 sisp

Ha, just realized it's you @sisp 😄Nice seeing you here and on Copier's repo!

pawamoy avatar Oct 05 '22 16:10 pawamoy

Haha, yes, it's a small world it seems. :rofl: I recognized you a while ago on Copier's repo after we had been discussing here for a bit. :laughing: mkdocstrings and the projects around it are awesome by the way! :pray:

sisp avatar Oct 05 '22 16:10 sisp

Thanks! :smile:

pawamoy avatar Oct 05 '22 20:10 pawamoy

Hi all, just adding to this that griffe (v0.22.2) still raises an error if or is used in the Returns section.

dekromp avatar Oct 26 '22 09:10 dekromp