Closed steven-douilliet closed 2 months ago
Similar to #91, this is a bug in PySMI where it should further analyze the generated types and avoid duplicate inheritance.
While the bugfix won't come soon, the manual workaround is also simple,
RMON2-MIB.py
in a text editor.class LastCreateTime(TextualConvention, TimeStamp)
to class LastCreateTime(TimeStamp)
.BTW, please switch from pysnmp-lextudio
to pysnmp
, as we won't update the former any more.
Close it now as won't work on it in near future.
We ran into this issue too, with the MIB compilation step in our application after switching to Python 3.12.
Manually fixing the .py file was no full work-around, because we generate the .py files on application startup in our Docker container, based on a plugin system (where each plugin can provide extra MIBs when required). Therefore, I automated the process of fixing the code, by leveraging the Python ast
package.
As possible inspiration for others that run into the same issue, here the gist of the method in our compiler that takes care of fixing the generate pysnmp MIB files that are stored in the output_path
directory. When there are more patterns to fix, these can be added quickly to this skeleton.
import ast
from pathlib import Path
class VisitorThatFixesMroIssue(ast.NodeTransformer):
def __init__(self) -> None:
super().__init__()
self.did_fix_code = False
def visit_ClassDef(self, node: ast.ClassDef) -> ast.AST:
base_names = [base.id for base in node.bases if isinstance(base, ast.Name)]
if base_names == ['TextualConvention', 'DisplayString']:
node.bases = [node.bases[1]]
self.did_fix_code = True
return self.generic_visit(node)
def fix_mib_py_file(file_path: Path) -> None:
tree = ast.parse(source=file_path.read_text())
visitor = VisitorThatFixesMroIssue()
visitor.visit(tree)
if visitor.did_fix_code:
file_path.write_text(ast.unparse(tree))
Just an idea
A quick fix for the pysmi code, might be to apply the above change right from the code generating template at https://github.com/lextudio/pysmi/blob/main/pysmi/codegen/templates/pysnmp/mib-definitions.j2#L219 The generating template code could could be something like this:
{%if definition['type']['type'] == 'DisplayString' %}
class {{ symbol }}({{ definition['type']['type'] }}):
{% else %}
class {{ symbol }}(TextualConvention, {{ definition['type']['type'] }}):
{% endif %}
Should have been fixed in PySMI release 1.5.
Yes, I can confirm that it works for the issues that I ran into. Upgrading to 1.5.0 fully vanished them. Thanks!
Hello, That it works! Thanks
Environment
Steps to Reproduce
Expected Behavior
LLDP-MIB::lldpRemTable
is resolved and the table entries are returned. For example (using linux commandsnmptable
):Observed Behavior
A load error is occured with root cause the following python error:
The root cause is the
TypeError: Cannot create a consistent method resolution
, which indicates an inheritance issue.Please note that when using another MIB, such as
IF-MIB::ifTable
, the resolution works correctly and no errors occur.