github / github/codeql

LGTM.com - false positive - Python: Class may not be initialized properly as method is not called from its __init__ method

Đang mở
#9,392 2 bình luận 0 reaction 0 người được giao Xem trên GitHub
false-positive
Ngôn ngữ chính
CodeQL
Star
10.1k
Fork
2.1k
Merge trung bình
2 ngày 15 giờ
Pull request đã merge (30 ngày)
141

Mô tả

**Description of the false positive**

In python when a class inherits from multiple classes, if any of these classes does not have an `__init__` method, the `__init__` of the next class will be called. It seems this is not correctly considered, leading to a false positive for https://lgtm.com/query/rule:5990072/lang:python/

**URL to the alert on the project page on LGTM.com**

Two examples of this false positive are:

- https://lgtm.com/projects/g/omni-us/jsonargparse/snapshot/c57ebe0a019e3aa2809a974b727063cc99a2f894/files/jsonargparse/core.py?sort=name&dir=ASC&mode=heatmap#xd619df233fb609ab:1
- https://lgtm.com/projects/g/omni-us/jsonargparse/snapshot/c57ebe0a019e3aa2809a974b727063cc99a2f894/files/jsonargparse/core.py?sort=name&dir=ASC&mode=heatmap#xff377f03629481f2:1

Note that if the mentioned `__init__` methods wouldn't be called, then the code would not work at all. Anyway, it is easy to debug and see that these are correctly called.

I also reproduced this locally by running `codeql database create --language=python` and `codeql database analyze` for the query [MissingCallToInit.ql](https://lgtm.com/query/rule:5990072/lang:python/) and the code snippet below:

```python
import argparse

class ClassWithoutInit:
def some_method(self):
...

class ClassWithInit:
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)

class ActionsContainer(ClassWithoutInit, ClassWithInit, argparse._ActionsContainer):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)

class ArgumentGroup(ActionsContainer, argparse._ArgumentGroup):
new_attr: str = None

parser = argparse.ArgumentParser()
group = ArgumentGroup(parser, 'abc')
```

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Đánh giá

Issue này chưa được đánh giá.

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.