Re: [PATCH v8 11/13] tools: netlink_yml_parser.py: add line numbers to parsed data

Mauro Carvalho Chehab
2025-07-09T15:44:52Z

Em Fri, 27 Jun 2025 12:03:07 +0100
Donald Hunter <donald.hunter@gmail.com> escreveu:

> Mauro Carvalho Chehab <mchehab+huawei@kernel.org> writes:
>
> > When something goes wrong, we want Sphinx error to point to the
> > right line number from the original source, not from the
> > processed ReST data.
> >
> > Signed-off-by: Mauro Carvalho Chehab <mchehab+huawei@kernel.org>
> > ---
> > tools/net/ynl/pyynl/lib/doc_generator.py | 34 ++++++++++++++++++++++--
> > 1 file changed, 32 insertions(+), 2 deletions(-)
> >
> > diff --git a/tools/net/ynl/pyynl/lib/doc_generator.py b/tools/net/ynl/pyynl/lib/doc_generator.py
> > index 866551726723..a9d8ab6f2639 100644
> > --- a/tools/net/ynl/pyynl/lib/doc_generator.py
> > +++ b/tools/net/ynl/pyynl/lib/doc_generator.py
> > @@ -20,6 +20,16 @@
> > from typing import Any, Dict, List
> > import yaml
> >
> > +LINE_STR = '__lineno__'
> > +
> > +class NumberedSafeLoader(yaml.SafeLoader):
> > + """Override the SafeLoader class to add line number to parsed data"""
> > +
> > + def construct_mapping(self, node):
> > + mapping = super().construct_mapping(node)
> > + mapping[LINE_STR] = node.start_mark.line
> > +
> > + return mapping
>
> pylint gives these 2 warnings:
>
> tools/net/ynl/pyynl/lib/doc_generator.py:25:0: R0901: Too many ancestors (9/7) (too-many-ancestors)

I'm yet to find any pylint Rxxx warning that I didn't have to
disable ;-)

This particular one is useless for us, as it basically tells that PyYAML
has a big class hierarchy.

> tools/net/ynl/pyynl/lib/doc_generator.py:28:4: W0221: Number of parameters was 3 in 'SafeConstructor.construct_mapping' and is now 2 in overriding 'NumberedSafeLoader.construct_mapping' method (arguments-differ)

I'll fix this one to prevent potential future issues.

Changing the code to:

-class NumberedSafeLoader(yaml.SafeLoader):
+class NumberedSafeLoader(yaml.SafeLoader): # pylint: disable=R0901
"""Override the SafeLoader class to add line number to parsed data"""

- def construct_mapping(self, node):
- mapping = super().construct_mapping(node)
+ def construct_mapping(self, node, *args, **kwargs):
+ mapping = super().construct_mapping(node, *args, **kwargs)
mapping[LINE_STR] = node.start_mark.line

return mapping

This should hopefully be future-proof.

Regards,
Mauro