python - Pyreverse 没有检测到父类是 Composite...为什么?

标签 python oop pylint composite pyreverse

假设我有以下目录结构:

Folder PATH listing
Volume serial number is 986C-80E1
C:.
    test.py
    __init__.py
    
No subfolders exist 

__init__.py 为空。在test.py中。我有以下代码:

class Employee:
    def __init__(self, identifier, name, address):
        self.address = None

class Manager(Employee):
    pass

class Secretary(Employee):
    pass

class Address:
    def __init__(self, street_num, street_name):
        self.street_num = street_num
        self.street_name = street_name

这里的想法是实现简单的一对多组合。也就是说,所有 Employee 子类型还包含一个 Address 实例。

然后我运行 pyreverse -S -o uml.png . 生成 UML 类图并获得以下内容:

enter image description here

pyreverse 无法识别 EmployeeAddress 之间存在复合组件关系。但是,如果我将 test.py 重构为以下内容:

class Employee:
    def __init__(self, identifier, name):
        self.address = None

class Manager(Employee):
    pass

class Secretary(Employee):
    pass

class Address:
    def __init__(self, street_num, street_name):
        self.street_num = street_num
        self.street_name = street_name

bob = Manager(1, "Bob")
bob.address =  Address(1, "main ave")

我知道地址Manager有关联。

enter image description here

这在技术上不是不正确吗?据我了解,该关联应该与父类Employee相关。为什么pyreverse认为Address只是Manager的一个组件?

最佳答案

pyreverse 检测到您已实例化 Manager,这就是为什么它认为 AddressManager 的一部分。如果你做了 type hint在您的构造函数中,pyreverse 应该检测 EmployeeAddress 之间的正确关系。

关于python - Pyreverse 没有检测到父类是 Composite...为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70323980/

相关文章:

java - 设计面向对象的系统/GUI

使用子类型类的 C# 接口(interface)

php - 在 php 中正确实现模型类

python - pylint-pytest 如何抛出 F6401 Can-enumerate-pytest-fixtures

python - 与 matplotlib 计时器的混淆

python - 使用Python搜索json并返回列表

python - Pylint 在没有导入的文件中报告循环导入

python - 通过 pylint 处理用户提出的弃用警告

python - 来自多个数据帧的 Pandas 条形图

python - 如何根据 PySpark 中的条件修改行子集