@@ -629,6 +629,25 @@ def _join_dirs_and_files(dirnames: ty.List[AnyStr], filenames: ty.List[AnyStr])
629
629
for filename in filenames :
630
630
yield filename , False
631
631
632
+ @staticmethod
633
+ def _walk_separator (
634
+ matcher : Matcher [AnyStr ],
635
+ directory_str : ty .Optional [AnyStr ]
636
+ ) -> ty .Union [bytes , str ]:
637
+ """
638
+ Determine which separator to use.
639
+
640
+ Because os.fsencode can return a byte array, we must allow returning a byte array,
641
+ regardless of AnyType.
642
+ """
643
+
644
+ if directory_str is not None :
645
+ return utils .maybe_fsencode (os .path .sep , directory_str )
646
+ elif matcher is not None and matcher .is_binary :
647
+ return os .fsencode (os .path .sep )
648
+ else :
649
+ return os .path .sep
650
+
632
651
@staticmethod
633
652
def _walk_wide (
634
653
dot : AnyStr ,
@@ -655,12 +674,11 @@ def _walk(
655
674
follow_symlinks : bool ,
656
675
intermediate_dirs : bool
657
676
) -> ty .Generator [FSNodeEntry , ty .Any , None ]:
658
- if directory_str is not None :
659
- sep = utils .maybe_fsencode (os .path .sep , directory_str )
660
- elif matcher is not None and matcher .is_binary :
661
- sep = os .fsencode (os .path .sep ) # type: ignore[assignment]
662
- else :
663
- sep = os .path .sep # type: ignore[assignment]
677
+ separator = self ._walk_separator (matcher = matcher , directory_str = directory_str )
678
+
679
+ # TODO: Because os.fsencode can return a byte array, we need to refactor how we use 'sep'
680
+ sep : AnyStr = separator # type: ignore[assignment]
681
+
664
682
dot = utils .maybe_fsencode ("." , sep )
665
683
666
684
# Identify the leading portion of the `dirpath` returned by `os.walk`
0 commit comments