88from collections .abc import Callable , Mapping , Sequence
99from copy import deepcopy
1010from dataclasses import field
11- from datetime import datetime
11+ from datetime import datetime , timezone
1212from functools import cached_property
1313from hashlib import sha512
1414from os import urandom
@@ -47,7 +47,7 @@ def _now() -> datetime:
4747 "strftime format reference https://strftime.org/" ,
4848 FutureWarning ,
4949 )
50- return datetime .utcnow ( )
50+ return datetime .now ( tz = timezone . utc () )
5151
5252
5353def _make_secret () -> str :
@@ -368,9 +368,8 @@ def _formatted_choices(self) -> Sequence[Choice]:
368368
369369 def get_message (self ) -> str :
370370 """Get the message that will be printed to the user."""
371- if self .help :
372- if rendered_help := self .render_value (self .help ):
373- return force_str_end (rendered_help ) + " "
371+ if self .help and (rendered_help := self .render_value (self .help )):
372+ return force_str_end (rendered_help ) + " "
374373 # Otherwise, there's no help message defined.
375374 message = self .var_name
376375 if (answer_type := self .get_type_name ()) != "str" :
@@ -387,11 +386,11 @@ def get_questionary_structure(self) -> AnyByStrDict: # noqa: C901
387386 def _validate (answer : str ) -> str | Literal [True ]:
388387 try :
389388 ans = self .parse_answer (answer )
390- except Exception :
389+ except Exception : # noqa: BLE001
391390 return "Invalid input"
392391 try :
393392 self .validate_answer (ans )
394- except Exception as exc :
393+ except Exception as exc : # noqa: BLE001
395394 return str (exc )
396395 return True
397396
@@ -460,7 +459,7 @@ def validate_answer(self, answer: Any) -> None:
460459 """Validate user answer."""
461460 try :
462461 err_msg = self .render_value (self .validator , {self .var_name : answer }).strip ()
463- except Exception as error :
462+ except Exception as error : # noqa: BLE001
464463 err_msg = str (error )
465464 if err_msg :
466465 raise ValueError (
@@ -557,12 +556,12 @@ def parse_yaml_list(string: str) -> list[str]:
557556 The parsed list of raw items.
558557
559558 Raises:
560- ValueError : If the YAML string is not a list.
559+ TypeError : If the YAML string is not a list.
561560 """
562561 node = yaml .compose (string , Loader = yaml .SafeLoader )
563562
564563 if not isinstance (node , yaml .nodes .SequenceNode ):
565- raise ValueError (f"Not a YAML list: { string !r} " )
564+ raise TypeError (f"Not a YAML list: { string !r} " )
566565
567566 items = []
568567 for item in node .value :
@@ -571,12 +570,13 @@ def parse_yaml_list(string: str) -> list[str]:
571570 if (
572571 isinstance (item , yaml .nodes .ScalarNode )
573572 and item .tag == "tag:yaml.org,2002:str"
574- ):
575573 # Strip quotes if the value is quoted to avoid double-quoting.
576- if (raw .startswith ('"' ) and raw .endswith ('"' )) or (
577- raw .startswith ("'" ) and raw .endswith ("'" )
578- ):
579- raw = raw [1 :- 1 ]
574+ and (
575+ (raw .startswith ('"' ) and raw .endswith ('"' ))
576+ or (raw .startswith ("'" ) and raw .endswith ("'" ))
577+ )
578+ ):
579+ raw = raw [1 :- 1 ]
580580
581581 items .append (raw )
582582
0 commit comments