From 70bf51a12568eda67cfe6639c598085b174bd86e Mon Sep 17 00:00:00 2001 From: karlicoss Date: Fri, 27 Oct 2023 21:28:38 +0100 Subject: [PATCH] core/stats: exclude contextmanagers from guess_stats --- my/core/common.py | 4 ++++ my/core/tests/auto_stats.py | 10 ++++++++-- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/my/core/common.py b/my/core/common.py index 602f8af..ccebaf2 100644 --- a/my/core/common.py +++ b/my/core/common.py @@ -409,6 +409,10 @@ def stat( ) -> Stats: if callable(func): fr = func() + if hasattr(fr, '__enter__') and hasattr(fr, '__exit__'): + # context managers has Iterable type, but they aren't data providers + # sadly doesn't look like there is a way to tell from typing annotations + return {} fname = func.__name__ else: # meh. means it's just a list.. not sure how to generate a name then diff --git a/my/core/tests/auto_stats.py b/my/core/tests/auto_stats.py index 2946ab2..2c09b5b 100644 --- a/my/core/tests/auto_stats.py +++ b/my/core/tests/auto_stats.py @@ -1,11 +1,11 @@ """ Helper 'module' for test_guess_stats """ - +from contextlib import contextmanager from dataclasses import dataclass from datetime import datetime, timedelta from pathlib import Path -from typing import Iterable, Sequence +from typing import Iterable, Sequence, Iterator @dataclass @@ -28,3 +28,9 @@ def iter_data() -> Iterable[Item]: for path in inputs(): for i in range(3): yield Item(id=str(i), dt=dt + timedelta(days=i), source=path) + + +@contextmanager +def some_contextmanager() -> Iterator[str]: + # this shouldn't end up in guess_stats because context manager is not a data provider + yield 'hello'