bmw-software-engineering / lobster

Lightweight Open BMW Software Traceability Evidence Report
GNU Affero General Public License v3.0
12 stars 6 forks source link

Handling duplicate function definitions #30

Closed phiwuu closed 1 month ago

phiwuu commented 5 months ago

Current Behaviour

If a Python file contains several function definitions with the same name, then the output of lobster-python cannot be forwarded to lobster-report.

Consider the following Python file (call it code.py):

value = False

if value:
    def compute():
        return 42
else:
    def compute():
        return 43

print(compute())

Now run lobster-python:

> lobster-python code.py --out="python.lobster"

The result of python.lobster is the following:

{
  "data": [
    {
      "tag": "python code.compute",
      "location": {
        "kind": "file",
        "file": "code.py",
        "line": 4,
        "column": null
      },
      "name": "code.compute",
      "messages": [],
      "just_up": [],
      "just_down": [],
      "just_global": [],
      "language": "Python",
      "kind": "Function"
    },
    {
      "tag": "python code.compute",
      "location": {
        "kind": "file",
        "file": "code.py",
        "line": 8,
        "column": null
      },
      "name": "code.compute",
      "messages": [],
      "just_up": [],
      "just_down": [],
      "just_global": [],
      "language": "Python",
      "kind": "Function"
    }
  ],
  "generator": "lobster_python",
  "schema": "lobster-imp-trace",
  "version": 3
}

Now lobster-report cannot handle this input, as it detects a duplicate definition of "code.compute". The error is raised here: https://github.com/bmw-software-engineering/lobster/blob/666f8afed28a7e4a5156fc7cacb47f2b6998c622/lobster/io.py#L128

Expected Behaviour

Now the question is: What is the expected behaviour? For sure the above Python code is valid and needs to be supported by lobster. On the other hand, duplicate definitions shall be detected, too. We could add a unique identifier to the function name, like the line number, but then it will be hard to reference this function in other elements of the tracing policy. But the V-model of ISO 26262 always recommends to trace from code to requirements, so I wonder if tracing from something to python code through lobster-links is a real use case. Then adding line numbers might be a valid solution. This could be implemented as a switch flag.

Any ideas how to handle this best?

phiwuu commented 5 months ago

Real-world example: https://github.com/python-attrs/cattrs/blob/v22.1.0/src/cattrs/_compat.py#L184

christophkloeffel commented 2 months ago

we could just add the line_nr at the end of the current Tracing Tag to make it unique. this is done in the cpp converter as well in https://github.com/bmw-software-engineering/lobster/blob/2b8296c0b7e8e71089872ec96c3bd5299eeed033/lobster/tools/cpp/cpp.py#L132

phiwuu commented 2 months ago

Adding the line number works to solve the bug issues, but it introduces another problem. Consider some project uses a test-driven approach and breaks system tests down into unit tests (instead of breaking down system requirements into unit requirements), and they want to link the unit test against the system test. This is no longer possible, because the line number of the system test will change easily.