Create a string representation of the Signature object. If *max_width* integer is passed, signature will try to fit into the *max_width*. If signature is longer than *max_width*, all parameters will be on separate lines. If *quote_annotation_strings* is Fals
(self, *, max_width=None, quote_annotation_strings=True)
| 3263 | return self.format() |
| 3264 | |
| 3265 | def format(self, *, max_width=None, quote_annotation_strings=True): |
| 3266 | """Create a string representation of the Signature object. |
| 3267 | |
| 3268 | If *max_width* integer is passed, |
| 3269 | signature will try to fit into the *max_width*. |
| 3270 | If signature is longer than *max_width*, |
| 3271 | all parameters will be on separate lines. |
| 3272 | |
| 3273 | If *quote_annotation_strings* is False, annotations |
| 3274 | in the signature are displayed without opening and closing quotation |
| 3275 | marks. This is useful when the signature was created with the |
| 3276 | STRING format or when ``from __future__ import annotations`` was used. |
| 3277 | """ |
| 3278 | result = [] |
| 3279 | render_pos_only_separator = False |
| 3280 | render_kw_only_separator = True |
| 3281 | for param in self.parameters.values(): |
| 3282 | formatted = param._format(quote_annotation_strings=quote_annotation_strings) |
| 3283 | |
| 3284 | kind = param.kind |
| 3285 | |
| 3286 | if kind == _POSITIONAL_ONLY: |
| 3287 | render_pos_only_separator = True |
| 3288 | elif render_pos_only_separator: |
| 3289 | # It's not a positional-only parameter, and the flag |
| 3290 | # is set to 'True' (there were pos-only params before.) |
| 3291 | result.append('/') |
| 3292 | render_pos_only_separator = False |
| 3293 | |
| 3294 | if kind == _VAR_POSITIONAL: |
| 3295 | # OK, we have an '*args'-like parameter, so we won't need |
| 3296 | # a '*' to separate keyword-only arguments |
| 3297 | render_kw_only_separator = False |
| 3298 | elif kind == _KEYWORD_ONLY and render_kw_only_separator: |
| 3299 | # We have a keyword-only parameter to render and we haven't |
| 3300 | # rendered an '*args'-like parameter before, so add a '*' |
| 3301 | # separator to the parameters list ("foo(arg1, *, arg2)" case) |
| 3302 | result.append('*') |
| 3303 | # This condition should be only triggered once, so |
| 3304 | # reset the flag |
| 3305 | render_kw_only_separator = False |
| 3306 | |
| 3307 | result.append(formatted) |
| 3308 | |
| 3309 | if render_pos_only_separator: |
| 3310 | # There were only positional-only parameters, hence the |
| 3311 | # flag was not reset to 'False' |
| 3312 | result.append('/') |
| 3313 | |
| 3314 | rendered = '({})'.format(', '.join(result)) |
| 3315 | if max_width is not None and len(rendered) > max_width: |
| 3316 | rendered = '(\n {}\n)'.format(',\n '.join(result)) |
| 3317 | |
| 3318 | if self.return_annotation is not _empty: |
| 3319 | anno = formatannotation(self.return_annotation, |
| 3320 | quote_annotation_strings=quote_annotation_strings) |
| 3321 | rendered += ' -> {}'.format(anno) |
| 3322 |