(
self,
func: Function,
params: dict[int, Parameter],
)
| 106 | return libclinic.normalize_snippet(code) |
| 107 | |
| 108 | def deprecate_positional_use( |
| 109 | self, |
| 110 | func: Function, |
| 111 | params: dict[int, Parameter], |
| 112 | ) -> str: |
| 113 | assert len(params) > 0 |
| 114 | first_pos = next(iter(params)) |
| 115 | last_pos = next(reversed(params)) |
| 116 | |
| 117 | # Format the deprecation message. |
| 118 | if len(params) == 1: |
| 119 | condition = f"nargs == {first_pos+1}" |
| 120 | amount = f"{first_pos+1} " if first_pos else "" |
| 121 | pl = "s" |
| 122 | else: |
| 123 | condition = f"nargs > {first_pos} && nargs <= {last_pos+1}" |
| 124 | amount = f"more than {first_pos} " if first_pos else "" |
| 125 | pl = "s" if first_pos != 1 else "" |
| 126 | message = ( |
| 127 | f"Passing {amount}positional argument{pl} to " |
| 128 | f"{func.fulldisplayname}() is deprecated." |
| 129 | ) |
| 130 | |
| 131 | for (major, minor), group in itertools.groupby( |
| 132 | params.values(), key=attrgetter("deprecated_positional") |
| 133 | ): |
| 134 | names = [repr(p.name) for p in group] |
| 135 | pstr = libclinic.pprint_words(names) |
| 136 | if len(names) == 1: |
| 137 | message += ( |
| 138 | f" Parameter {pstr} will become a keyword-only parameter " |
| 139 | f"in Python {major}.{minor}." |
| 140 | ) |
| 141 | else: |
| 142 | message += ( |
| 143 | f" Parameters {pstr} will become keyword-only parameters " |
| 144 | f"in Python {major}.{minor}." |
| 145 | ) |
| 146 | |
| 147 | # Append deprecation warning to docstring. |
| 148 | docstring = textwrap.fill(f"Note: {message}") |
| 149 | func.docstring += f"\n\n{docstring}\n" |
| 150 | # Format and return the code block. |
| 151 | code = self.DEPRECATION_WARNING_PROTOTYPE.format( |
| 152 | condition=condition, |
| 153 | errcheck="", |
| 154 | message=libclinic.wrapped_c_string_literal(message, width=64, |
| 155 | subsequent_indent=20), |
| 156 | ) |
| 157 | return libclinic.normalize_snippet(code, indent=4) |
| 158 | |
| 159 | def deprecate_keyword_use( |
| 160 | self, |
no test coverage detected