Skip to content

Commit

Permalink
Merge pull request #247 from SCM-NV/getattr
Browse files Browse the repository at this point in the history
REV: Reintroduce support for python 3.6
  • Loading branch information
BvB93 committed Jul 6, 2021
2 parents 2ee3d22 + 5e2ba6d commit dfaf36b
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 20 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/pythonapp.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
fail-fast: false
matrix:
os: [macos-latest, ubuntu-latest]
version: [3.7, 3.8, 3.9]
version: [3.6, 3.7, 3.8, 3.9]

steps:
- uses: actions/checkout@v2
Expand Down
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,10 @@ def readme():
"qmflows": ['data/dictionaries/*yaml',
'py.typed']
},
python_requires='>=3.7',
python_requires='>=3.6',
classifiers=[
'Intended Audience :: Science/Research',
'Programming Language :: Python :: 3.6',
'Programming Language :: Python :: 3.7',
'Programming Language :: Python :: 3.8',
'Programming Language :: Python :: 3.9',
Expand Down
41 changes: 23 additions & 18 deletions src/qmflows/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""QMFlows API."""

from __future__ import annotations
import sys
from typing import TYPE_CHECKING

from .__version__ import __version__

Expand Down Expand Up @@ -31,20 +32,24 @@
'freq', 'geometry', 'singlepoint', 'ts', 'md', 'cell_opt',
'find_first_job', 'select_max', 'select_min']

_TEMPLATES = frozenset(templates.__all__)
_DIR_CACHE: None | list[str] = None


def __getattr__(name: str) -> Settings:
"""Ensure that the qmflows templates are always copied before returning."""
if name in _TEMPLATES:
return getattr(templates, name).copy()
raise AttributeError(f"module {__name__} has no attribute {name}")


def __dir__() -> list[str]:
"""Manually insert the qmflows templates into :func:`dir`."""
global _DIR_CACHE
if _DIR_CACHE is None:
_DIR_CACHE = sorted(list(globals()) + templates.__all__)
return _DIR_CACHE
if TYPE_CHECKING or sys.version_info < (3, 7):
from .templates import freq, geometry, singlepoint, ts, md, cell_opt
else:
_TEMPLATES = frozenset(templates.__all__)
_DIR_CACHE: "None | list[str]" = None

def __getattr__(name: str) -> Settings:
"""Ensure that the qmflows templates are always copied before returning."""
if name in _TEMPLATES:
return getattr(templates, name).copy()
raise AttributeError(f"module {__name__} has no attribute {name}")

def __dir__() -> "list[str]":
"""Manually insert the qmflows templates into :func:`dir`."""
global _DIR_CACHE
if _DIR_CACHE is None:
_DIR_CACHE = sorted(list(globals()) + templates.__all__)
return _DIR_CACHE

# Clean up the namespace
del sys, TYPE_CHECKING
3 changes: 3 additions & 0 deletions test/test_templates.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
"""Test that the templates behave correctly."""

import sys
import qmflows
from qmflows import Settings, templates
from qmflows.templates import freq, geometry, singlepoint, ts
Expand All @@ -23,6 +25,7 @@ def test_templates():


@pytest.mark.parametrize("name", templates.__all__)
@pytest.mark.skipif(sys.version_info < (3, 7), reason="Requires python >= 3.7")
def test_id(name: str) -> None:
"""Test that getting a template returns a copy."""
s1 = getattr(qmflows, name)
Expand Down

0 comments on commit dfaf36b

Please sign in to comment.