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

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

54 statements  

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 Use apply_default decorator for the `default_args` feature to work properly; 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 Remove @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 TODO: Python 3.9+: Rewrite this to use ast.parse and ast.unparse 

67 """ 

68 

69 def _remove_task_decorator(py_source, decorator_name): 

70 # if no line starts with @decorator_name, we can early exit 

71 for line in py_source.split("\n"): 

72 if line.startswith(decorator_name): 

73 break 

74 else: 

75 return python_source 

76 split = python_source.split(decorator_name, 1) 

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

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

79 after_decorator = _balance_parens(after_decorator) 

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

81 after_decorator = after_decorator[1:] 

82 return before_decorator + after_decorator 

83 

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

85 for decorator in decorators: 

86 python_source = _remove_task_decorator(python_source, decorator) 

87 return python_source 

88 

89 

90def _balance_parens(after_decorator): 

91 num_paren = 1 

92 after_decorator = deque(after_decorator) 

93 after_decorator.popleft() 

94 while num_paren: 

95 current = after_decorator.popleft() 

96 if current == "(": 

97 num_paren = num_paren + 1 

98 elif current == ")": 

99 num_paren = num_paren - 1 

100 return "".join(after_decorator) 

101 

102 

103class _autostacklevel_warn: 

104 def __init__(self): 

105 self.warnings = __import__("warnings") 

106 

107 def __getattr__(self, name): 

108 return getattr(self.warnings, name) 

109 

110 def __dir__(self): 

111 return dir(self.warnings) 

112 

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

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

115 

116 

117def fixup_decorator_warning_stack(func): 

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

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

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

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