Skip to content

Commit

Permalink
Fix Type Constructor Classes in Code Level Metrics (#708)
Browse files Browse the repository at this point in the history
* Fix CLM exception catching

* Reorganize CLM Tests

* Add type constructor tests to CLM

* Fix line number

* Pin tox version

* Fix lambda tests in CLM

* Fix lint issues

* Turn helper func into pytest fixture

Co-authored-by: Hannah Stepanek <hstepanek@newrelic.com>
  • Loading branch information
TimPansino and hmstepanek committed Dec 8, 2022
1 parent 6a0c38c commit 15527e3
Show file tree
Hide file tree
Showing 4 changed files with 340 additions and 142 deletions.
2 changes: 1 addition & 1 deletion .github/actions/setup-python-matrix/action.yml
Expand Up @@ -47,4 +47,4 @@ runs:
shell: bash
run: |
python3.10 -m pip install -U pip
python3.10 -m pip install -U wheel setuptools tox virtualenv!=20.0.24
python3.10 -m pip install -U wheel setuptools 'tox<4' virtualenv!=20.0.24
2 changes: 1 addition & 1 deletion newrelic/core/code_level_metrics.py
Expand Up @@ -89,7 +89,7 @@ def extract_code_from_callable(func):
# Use inspect to get file and line number
file_path = inspect.getsourcefile(func)
line_number = inspect.getsourcelines(func)[1]
except TypeError:
except Exception:
pass

# Split function path to extract class name
Expand Down
41 changes: 38 additions & 3 deletions tests/agent_features/_test_code_level_metrics.py
Expand Up @@ -13,11 +13,12 @@
# limitations under the License.
import functools


def exercise_function():
return


class ExerciseClass():
class ExerciseClass(object):
def exercise_method(self):
return

Expand All @@ -30,12 +31,46 @@ def exercise_class_method(cls):
return


class ExerciseClassCallable():
class ExerciseClassCallable(object):
def __call__(self):
return


def exercise_method(self):
return


@staticmethod
def exercise_static_method():
return


@classmethod
def exercise_class_method(cls):
return


def __call__(self):
return


type_dict = {
"exercise_method": exercise_method,
"exercise_static_method": exercise_static_method,
"exercise_class_method": exercise_class_method,
"exercise_lambda": lambda: None,
}
callable_type_dict = type_dict.copy()
callable_type_dict["__call__"] = __call__

ExerciseTypeConstructor = type("ExerciseTypeConstructor", (object,), type_dict)
ExerciseTypeConstructorCallable = type("ExerciseTypeConstructorCallable", (object,), callable_type_dict)


CLASS_INSTANCE = ExerciseClass()
CLASS_INSTANCE_CALLABLE = ExerciseClassCallable()
TYPE_CONSTRUCTOR_CLASS_INSTANCE = ExerciseTypeConstructor()
TYPE_CONSTRUCTOR_CALLABLE_CLASS_INSTANCE = ExerciseTypeConstructorCallable()

exercise_lambda = lambda: None
exercise_lambda = lambda: None # noqa: E731
exercise_partial = functools.partial(exercise_function)

0 comments on commit 15527e3

Please sign in to comment.