Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix] fix pytorch2.0 not support sys._getframe to remove private key #979

Merged
merged 2 commits into from
Mar 14, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions mmengine/structures/base_data_element.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# Copyright (c) OpenMMLab. All rights reserved.
import copy
import sys
from typing import Any, Iterator, Optional, Tuple, Type, Union

import numpy as np
Expand Down Expand Up @@ -309,7 +308,16 @@ def keys(self) -> list:
Returns:
list: Contains all keys in data_fields.
"""
return list(self._data_fields)
# We assume that the name of the attribute related to property is
# '_' + the name of the property. We use this rule to filter out
# private keys.
zhouzaida marked this conversation as resolved.
Show resolved Hide resolved
zhouzaida marked this conversation as resolved.
Show resolved Hide resolved
# TODO: Use a more robust way to solve this problem
private_keys = {
'_' + key
for key in self._data_fields
if isinstance(getattr(type(self), key, None), property)
}
return list(self._data_fields - private_keys)

def metainfo_keys(self) -> list:
"""
Expand Down Expand Up @@ -466,12 +474,7 @@ def set_field(self,
raise AttributeError(
f'Cannot set {name} to be a field of data '
f'because {name} is already a metainfo field')
# The name only added to `data_fields` when it is not the
# attribute related to property(methods decorated by @property).
if not isinstance(
getattr(type(self),
sys._getframe(1).f_code.co_name, None), property):
self._data_fields.add(name)
self._data_fields.add(name)
super().__setattr__(name, value)

# Tensor-like methods
Expand Down