Coverage for /pythoncovmergedfiles/medio/medio/src/airflow/airflow/utils/decorators.py: 38%

52 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2023-06-07 06:35 +0000

1# 

2# Licensed to the Apache Software Foundation (ASF) under one 

3# or more contributor license agreements. See the NOTICE file 

4# distributed with this work for additional information 

5# regarding copyright ownership. The ASF licenses this file 

6# to you under the Apache License, Version 2.0 (the 

7# "License"); you may not use this file except in compliance 

8# with the License. You may obtain a copy of the License at 

9# 

10# http://www.apache.org/licenses/LICENSE-2.0 

11# 

12# Unless required by applicable law or agreed to in writing, 

13# software distributed under the License is distributed on an 

14# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 

15# KIND, either express or implied. See the License for the 

16# specific language governing permissions and limitations 

17# under the License. 

18from __future__ import annotations 

19 

20import sys 

21import warnings 

22from collections import deque 

23from functools import wraps 

24from typing import Callable, TypeVar, cast 

25 

26from airflow.exceptions import RemovedInAirflow3Warning 

27 

28T = TypeVar("T", bound=Callable) 

29 

30 

31def apply_defaults(func: T) -> T: 

32 """ 

33 This decorator is deprecated. 

34 

35 In previous versions, all subclasses of BaseOperator must use apply_default decorator for the" 

36 `default_args` feature to work properly. 

37 

38 In current version, it is optional. The decorator is applied automatically using the metaclass. 

39 """ 

40 warnings.warn( 

41 "This decorator is deprecated. \n" 

42 "\n" 

43 "In previous versions, all subclasses of BaseOperator must use apply_default decorator for the " 

44 "`default_args` feature to work properly.\n" 

45 "\n" 

46 "In current version, it is optional. The decorator is applied automatically using the metaclass.\n", 

47 RemovedInAirflow3Warning, 

48 stacklevel=3, 

49 ) 

50 

51 # Make it still be a wrapper to keep the previous behaviour of an extra stack frame 

52 @wraps(func) 

53 def wrapper(*args, **kwargs): 

54 return func(*args, **kwargs) 

55 

56 return cast(T, wrapper) 

57 

58 

59def remove_task_decorator(python_source: str, task_decorator_name: str) -> str: 

60 """ 

61 Removes @task or similar decorators as well as @setup and @teardown. 

62 

63 :param python_source: The python source code 

64 :param task_decorator_name: the decorator name 

65 """ 

66 

67 def _remove_task_decorator(py_source, decorator_name): 

68 if decorator_name not in py_source: 

69 return python_source 

70 split = python_source.split(decorator_name) 

71 before_decorator, after_decorator = split[0], split[1] 

72 if after_decorator[0] == "(": 

73 after_decorator = _balance_parens(after_decorator) 

74 if after_decorator[0] == "\n": 

75 after_decorator = after_decorator[1:] 

76 return before_decorator + after_decorator 

77 

78 decorators = ["@setup", "@teardown", task_decorator_name] 

79 for decorator in decorators: 

80 python_source = _remove_task_decorator(python_source, decorator) 

81 return python_source 

82 

83 

84def _balance_parens(after_decorator): 

85 num_paren = 1 

86 after_decorator = deque(after_decorator) 

87 after_decorator.popleft() 

88 while num_paren: 

89 current = after_decorator.popleft() 

90 if current == "(": 

91 num_paren = num_paren + 1 

92 elif current == ")": 

93 num_paren = num_paren - 1 

94 return "".join(after_decorator) 

95 

96 

97class _autostacklevel_warn: 

98 def __init__(self): 

99 self.warnings = __import__("warnings") 

100 

101 def __getattr__(self, name): 

102 return getattr(self.warnings, name) 

103 

104 def __dir__(self): 

105 return dir(self.warnings) 

106 

107 def warn(self, message, category=None, stacklevel=1, source=None): 

108 self.warnings.warn(message, category, stacklevel + 2, source) 

109 

110 

111def fixup_decorator_warning_stack(func): 

112 if func.__globals__.get("warnings") is sys.modules["warnings"]: 

113 # Yes, this is more than slightly hacky, but it _automatically_ sets the right stacklevel parameter to 

114 # `warnings.warn` to ignore the decorator. 

115 func.__globals__["warnings"] = _autostacklevel_warn()