From fb414f109d31fd7ee8a0a27b787db6f7908a76d5 Mon Sep 17 00:00:00 2001 From: Satyarth Agrahari Date: Thu, 9 Mar 2023 20:58:31 +0530 Subject: [PATCH 1/7] remove engine overwrite in pandas TextFileReader patch --- pyfakefs/patched_packages.py | 1 - 1 file changed, 1 deletion(-) diff --git a/pyfakefs/patched_packages.py b/pyfakefs/patched_packages.py index 88230b85..fb9ef153 100644 --- a/pyfakefs/patched_packages.py +++ b/pyfakefs/patched_packages.py @@ -117,7 +117,6 @@ def __init__(self, _): class TextFileReader(parsers.TextFileReader): def __init__(self, *args, **kwargs): - kwargs["engine"] = "python" super().__init__(*args, **kwargs) def __getattr__(self, name): From baaa52312e882cda2544d0db85ebe9a3517849cb Mon Sep 17 00:00:00 2001 From: Satyarth Agrahari Date: Thu, 9 Mar 2023 20:58:31 +0530 Subject: [PATCH 2/7] remove patching of TextFileReader for pandas for version >= 1.2 --- CHANGES.md | 2 ++ pyfakefs/patched_packages.py | 8 +++++++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/CHANGES.md b/CHANGES.md index dc9041e9..d2947170 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -9,6 +9,8 @@ The released versions correspond to PyPI releases. `fake_io.FakeIoModule` and `fake_open.FakeFileOpen`. Additionally, all fake file classes have been moved to `fake_file`. While most of the changes shall be upwards compatible, we cannot exclude that we missed some problems. +* Patching of parsers for pandas >= 1.2 is removed since pandas now uses Python fs functions + internally even when the engine selected is "c". ### Features * added possibility to set a path inaccessible under Windows by using `chown()` with diff --git a/pyfakefs/patched_packages.py b/pyfakefs/patched_packages.py index fb9ef153..ffc38322 100644 --- a/pyfakefs/patched_packages.py +++ b/pyfakefs/patched_packages.py @@ -17,6 +17,7 @@ import sys try: + import pandas as pd import pandas.io.parsers as parsers except ImportError: parsers = None @@ -94,7 +95,12 @@ def __getattr__(self, name): return getattr(self._xlrd_module, name) -if parsers is not None: +# From pandas v 1.2 onwards the python fs functions are used even when the engine selected is "c". +# This means that we don't explicitly have to change the engine. +patch_pandas = ( + parsers is not None and [int(v) for v in pd.__version__.split(".")] < [1, 2, 0] +) +if patch_pandas: # we currently need to add fake modules for both the parser module and # the contained text reader - maybe this can be simplified From 214ee1c0141aa7617e1007ff5d57c1d6b10064de Mon Sep 17 00:00:00 2001 From: Satyarth Agrahari Date: Wed, 15 Mar 2023 11:38:11 +0530 Subject: [PATCH 3/7] add back engine python to patched one --- pyfakefs/patched_packages.py | 1 + 1 file changed, 1 insertion(+) diff --git a/pyfakefs/patched_packages.py b/pyfakefs/patched_packages.py index ffc38322..b6d7fc22 100644 --- a/pyfakefs/patched_packages.py +++ b/pyfakefs/patched_packages.py @@ -123,6 +123,7 @@ def __init__(self, _): class TextFileReader(parsers.TextFileReader): def __init__(self, *args, **kwargs): + kwargs["engine"] = "python" super().__init__(*args, **kwargs) def __getattr__(self, name): From cec3fd051c2c78b33134ca549b6d086dce2ea034 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 15 Mar 2023 06:11:25 +0000 Subject: [PATCH 4/7] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- pyfakefs/patched_packages.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/pyfakefs/patched_packages.py b/pyfakefs/patched_packages.py index b6d7fc22..1079c55d 100644 --- a/pyfakefs/patched_packages.py +++ b/pyfakefs/patched_packages.py @@ -97,9 +97,11 @@ def __getattr__(self, name): # From pandas v 1.2 onwards the python fs functions are used even when the engine selected is "c". # This means that we don't explicitly have to change the engine. -patch_pandas = ( - parsers is not None and [int(v) for v in pd.__version__.split(".")] < [1, 2, 0] -) +patch_pandas = parsers is not None and [int(v) for v in pd.__version__.split(".")] < [ + 1, + 2, + 0, +] if patch_pandas: # we currently need to add fake modules for both the parser module and # the contained text reader - maybe this can be simplified From a21f59481f4c34d7e0d9aac755c08d3d9ac4e5b4 Mon Sep 17 00:00:00 2001 From: Satyarth Agrahari Date: Wed, 15 Mar 2023 11:49:08 +0530 Subject: [PATCH 5/7] fix flake8: comment too long --- pyfakefs/patched_packages.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pyfakefs/patched_packages.py b/pyfakefs/patched_packages.py index 1079c55d..d1fe203b 100644 --- a/pyfakefs/patched_packages.py +++ b/pyfakefs/patched_packages.py @@ -95,8 +95,8 @@ def __getattr__(self, name): return getattr(self._xlrd_module, name) -# From pandas v 1.2 onwards the python fs functions are used even when the engine selected is "c". -# This means that we don't explicitly have to change the engine. +# From pandas v 1.2 onwards the python fs functions are used even when the engine +# selected is "c". This means that we don't explicitly have to change the engine. patch_pandas = parsers is not None and [int(v) for v in pd.__version__.split(".")] < [ 1, 2, From a349b5840b0162cbfdf617ac6486f13d3b324744 Mon Sep 17 00:00:00 2001 From: Satyarth Agrahari Date: Wed, 15 Mar 2023 11:51:50 +0530 Subject: [PATCH 6/7] update all instances of parsers check with patch_pandas --- pyfakefs/patched_packages.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/pyfakefs/patched_packages.py b/pyfakefs/patched_packages.py index d1fe203b..2e1ac6cd 100644 --- a/pyfakefs/patched_packages.py +++ b/pyfakefs/patched_packages.py @@ -32,6 +32,13 @@ except ImportError: locks = None +# From pandas v 1.2 onwards the python fs functions are used even when the engine +# selected is "c". This means that we don't explicitly have to change the engine. +patch_pandas = parsers is not None and [int(v) for v in pd.__version__.split(".")] < [ + 1, + 2, + 0, +] def get_modules_to_patch(): modules_to_patch = {} @@ -44,14 +51,14 @@ def get_modules_to_patch(): def get_classes_to_patch(): classes_to_patch = {} - if parsers is not None: + if patch_pandas: classes_to_patch["TextFileReader"] = "pandas.io.parsers" return classes_to_patch def get_fake_module_classes(): fake_module_classes = {} - if parsers is not None: + if patch_pandas: fake_module_classes["TextFileReader"] = FakeTextFileReader return fake_module_classes @@ -95,13 +102,6 @@ def __getattr__(self, name): return getattr(self._xlrd_module, name) -# From pandas v 1.2 onwards the python fs functions are used even when the engine -# selected is "c". This means that we don't explicitly have to change the engine. -patch_pandas = parsers is not None and [int(v) for v in pd.__version__.split(".")] < [ - 1, - 2, - 0, -] if patch_pandas: # we currently need to add fake modules for both the parser module and # the contained text reader - maybe this can be simplified From 09bd17c364d0c1e3e734bd93cec2a0124463f842 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 15 Mar 2023 06:22:15 +0000 Subject: [PATCH 7/7] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- pyfakefs/patched_packages.py | 1 + 1 file changed, 1 insertion(+) diff --git a/pyfakefs/patched_packages.py b/pyfakefs/patched_packages.py index 2e1ac6cd..ba1a26b5 100644 --- a/pyfakefs/patched_packages.py +++ b/pyfakefs/patched_packages.py @@ -40,6 +40,7 @@ 0, ] + def get_modules_to_patch(): modules_to_patch = {} if xlrd is not None: