Skip to content

Commit 142ed5e

Browse files
committed
Workaround for strange type hint problem
- fixes 'Path | str' like type hints in wrapped functions inside an `fs` dependent fixture
1 parent 64d08c6 commit 142ed5e

File tree

3 files changed

+42
-0
lines changed

3 files changed

+42
-0
lines changed

CHANGES.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ The released versions correspond to PyPI releases.
1717
* changed the default for `FakeFilesystem.shuffle_listdir_results` to `True` to reflect
1818
the real filesystem behavior
1919

20+
### Fixes
21+
* fixes a problem with `Path` type hints using the pipe symbol in wrapped functions
22+
inside an `fs` dependent fixture (see [#1242](../../issues/1242))
23+
2024
## [Version 5.10.2](https://pypi.python.org/pypi/pyfakefs/5.10.2) (2025-11-04)
2125
Fixes a problem with `pathlib.glob` in Python 3.14.
2226

pyfakefs/fake_pathlib.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
from pathlib import PurePath
3939

4040
from collections.abc import Callable
41+
from typing import Any, Union
4142
from unittest import mock
4243
from urllib.parse import quote_from_bytes as urlquote_from_bytes
4344

@@ -1026,6 +1027,12 @@ def __init__(self, filesystem=None, from_patcher=False):
10261027
if self.fake_pathlib is None:
10271028
self.__class__.fake_pathlib = FakePathlibModule(filesystem, from_patcher)
10281029

1030+
def __or__(self, other: Any) -> Any:
1031+
# workaround for #1242 - pytest chokes on Path | ... type hint in wrapped function
1032+
return Union[self, other]
1033+
1034+
__ror__ = __or__
1035+
10291036
@property
10301037
def skip_names(self):
10311038
return [] # not used, here to allow a setter
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
from pathlib import Path
2+
3+
import pytest
4+
5+
# Regression test for #1242. Using a type hint in a wrapped function in a fixture dependent
6+
# on the 'fs' fixture that uses the union of 'Path' with another type using pipe symbol caused
7+
# "TypeError: unsupported operand type(s) for |: 'FakePathlibPathModule' and 'type'"
8+
9+
10+
@pytest.fixture
11+
def wrapper1(fs):
12+
def wrapped(path: Path | str) -> Path | str:
13+
return path
14+
15+
return wrapped
16+
17+
18+
@pytest.fixture
19+
def wrapper2(fs):
20+
def wrapped(path: str | Path) -> Path | str:
21+
return path
22+
23+
return wrapped
24+
25+
26+
def test_wrapped1(wrapper1):
27+
assert wrapper1("test_folder") == "test_folder"
28+
29+
30+
def test_wrapped2(wrapper2):
31+
assert wrapper2(Path("test_folder")) == Path("test_folder")

0 commit comments

Comments
 (0)