Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Prev Previous commit
Next Next commit
Fix failing type check
  • Loading branch information
Marcel Weiler committed Nov 7, 2023
commit 1aacfcc7bd3798b936f640de150026c315d0d9d0
8 changes: 4 additions & 4 deletions mypy/stubgenc.py
Original file line number Diff line number Diff line change
Expand Up @@ -508,14 +508,14 @@ def is_classmethod(self, class_info: ClassInfo, name: str, obj: object) -> bool:
return inspect.ismethod(obj)

def is_staticmethod(self, class_info: ClassInfo | None, name: str, obj: object) -> bool:
if self.is_c_module:
if class_info is None:
return False
elif self.is_c_module:
raw_lookup = getattr(class_info.cls, "__dict__") # noqa: B009
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why has this been # noqa'd? Wouldn't it be cleaner to do this, as the linter suggests?

Suggested change
raw_lookup = getattr(class_info.cls, "__dict__") # noqa: B009
raw_lookup = class_info.cls.__dict__

Copy link
Copy Markdown
Contributor Author

@WeilerMarcel WeilerMarcel Nov 9, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be honest, I wrote it that way because there are two other places in stubgenc.py where this pattern was used:

  • mypy/mypy/stubgenc.py

    Lines 453 to 455 in 8121e3c

    def get_members(self, obj: object) -> list[tuple[str, Any]]:
    obj_dict: Mapping[str, Any] = getattr(obj, "__dict__") # noqa: B009
    results = []
  • mypy/mypy/stubgenc.py

    Lines 718 to 725 in 8121e3c

    def generate_class_stub(self, class_name: str, cls: type, output: list[str]) -> None:
    """Generate stub for a single class using runtime introspection.
    The result lines will be appended to 'output'. If necessary, any
    required names will be added to 'imports'.
    """
    raw_lookup = getattr(cls, "__dict__") # noqa: B009
    items = self.get_members(cls)

    I will check the file history to see if I can find a specific reason why getattr is used.

Copy link
Copy Markdown
Contributor Author

@WeilerMarcel WeilerMarcel Nov 10, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The getattr call originally comes from commit 6bcaf40 and was added to make typeshed happy:

mypy/mypy/stubgenc.py

Lines 140 to 142 in 6bcaf40

# typeshed gives obj.__dict__ the not quite correct type Dict[str, Any]
# (it could be a mappingproxy!), which makes mypyc mad, so obfuscate it.
obj_dict = getattr(obj, '__dict__') # type: Mapping[str, Any]

The # noqa was added in 4287af4:

obj_dict = getattr(obj, '__dict__') # type: Mapping[str, Any] # noqa

raw_value = raw_lookup.get(name, obj)
return type(raw_value).__name__ in ("staticmethod")
Comment thread
WeilerMarcel marked this conversation as resolved.
Outdated
else:
return class_info is not None and isinstance(
inspect.getattr_static(class_info.cls, name), staticmethod
)
return isinstance(inspect.getattr_static(class_info.cls, name), staticmethod)

@staticmethod
def is_abstract_method(obj: object) -> bool:
Expand Down