Skip to content

Commit 6dd34c8

Browse files
committed
Change order of arguments
1 parent d2460aa commit 6dd34c8

4 files changed

Lines changed: 30 additions & 24 deletions

File tree

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
[![Python](https://img.shields.io/badge/python-3.7+-blue.svg)](https://www.python.org/downloads/release/python-370/)
44
[![Tests](https://github.com/ashleve/pyrootutils/actions/workflows/test.yml/badge.svg?branch=main&event=push)](https://github.com/ashleve/pyrootutils/actions/workflows/test.yml)
5-
[![codecov](https://codecov.io/gh/ashleve/pyrootutils/branch/main/graph/badge.svg)](https://codecov.io/gh/ashleve/pyrootutils)
5+
[![Codecov](https://codecov.io/gh/ashleve/pyrootutils/branch/main/graph/badge.svg)](https://codecov.io/gh/ashleve/pyrootutils)
66
[![Issues](https://img.shields.io/github/issues/ashleve/pyrootutils)](https://github.com/ashleve/pyrootutils/issues)
77
[![License](https://img.shields.io/github/license/ashleve/pyrootutils)](https://github.com/ashleve/pyrootutils/blob/main/LICENSE)
88
[![Release](https://img.shields.io/pypi/v/pyrootutils)](pypi.org/project/pyrootutils/1.0.0/)
@@ -51,10 +51,10 @@ assert data_dir.exists(), f"path doesn't exist: {data_dir}"
5151
# set root directory
5252
pyrootutils.set_root(
5353
path=path # path to the root directory
54-
pythonpath=True, # add root directory to the PYTHONPATH (helps with imports)
55-
cwd=True, # set current working directory to the root directory (helps with filepaths)
5654
project_root_env_var=True, # set the PROJECT_ROOT environment variable to root directory
5755
dotenv=True, # load environment variables from .env if exists in root directory
56+
pythonpath=True, # add root directory to the PYTHONPATH (helps with imports)
57+
cwd=True, # change current working directory to the root directory (helps with filepaths)
5858
)
5959
```
6060

@@ -65,11 +65,11 @@ import pyrootutils
6565
# combines find_root() and set_root() into one method
6666
root = pyrootutils.setup_root(
6767
search_from=__file__,
68-
indicator=".git" # indicator of the root directory
69-
pythonpath=True,
70-
cwd=True,
68+
indicator=".pyproject.toml"
7169
project_root_env_var=True,
7270
dotenv=True,
71+
pythonpath=True,
72+
cwd=True,
7373
)
7474
```
7575

pyrootutils/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22

33
__all__ = ["find_root", "set_root", "setup_root"]
44

5-
__version__ = "1.0.2"
5+
__version__ = "1.0.3"

pyrootutils/pyrootutils.py

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -77,19 +77,19 @@ def find_root(
7777

7878
def set_root(
7979
path: Union[str, Path],
80-
pythonpath: bool = True,
81-
cwd: bool = True,
8280
project_root_env_var: bool = True,
8381
dotenv: bool = True,
82+
pythonpath: bool = True,
83+
cwd: bool = True,
8484
) -> None:
8585
"""Set given path as a project root.
8686
8787
Args:
8888
path (Union[str, Path]): project root path.
89-
pythonpath (bool, optional): whether to add project root to pythonpath.
90-
cwd (bool, optional): whether to set current working directory to project root.
9189
project_root_env_var (bool, optional): whether to set PROJECT_ROOT environment variable to project root.
9290
dotenv (bool, optional): whether to load .env file from project root.
91+
pythonpath (bool, optional): whether to add project root to pythonpath.
92+
cwd (bool, optional): whether to set current working directory to project root.
9393
9494
Raises:
9595
FileNotFoundError: if root path does not exist.
@@ -102,18 +102,18 @@ def set_root(
102102
if not os.path.exists(path):
103103
raise FileNotFoundError(f"Project root path does not exist: {path}")
104104

105-
if pythonpath:
106-
sys.path.insert(0, path)
107-
108-
if cwd:
109-
os.chdir(path)
110-
111105
if project_root_env_var:
112106
os.environ["PROJECT_ROOT"] = path
113107

114108
if dotenv:
115109
load_dotenv(os.path.join(path, ".env"))
116110

111+
if pythonpath:
112+
sys.path.insert(0, path)
113+
114+
if cwd:
115+
os.chdir(path)
116+
117117

118118
def setup_root(
119119
search_from: Union[str, Path],
@@ -124,24 +124,30 @@ def setup_root(
124124
".git",
125125
"pyproject.toml",
126126
),
127-
pythonpath: bool = True,
128-
cwd: bool = True,
129127
project_root_env_var: bool = True,
130128
dotenv: bool = True,
129+
pythonpath: bool = True,
130+
cwd: bool = True,
131131
) -> Path:
132132
"""Combines `get_root()` and `set_root()` into one method.
133133
134134
Args:
135135
search_from (str): path to folder to start search from.
136136
indicator (Union[str, Iterable[str]], optional): Project root indicator(s). Defaults to ".project-root".
137-
pythonpath (bool, optional): whether to add project root to pythonpath.
138-
cwd (bool, optional): whether to set current working directory to project root.
139137
project_root_env_var (bool, optional): whether to set PROJECT_ROOT environment variable to project root.
140138
dotenv (bool, optional): whether to load .env file from project root.
139+
pythonpath (bool, optional): whether to add project root to pythonpath.
140+
cwd (bool, optional): whether to set current working directory to project root.
141141
142142
Returns:
143143
Path: path to project root.
144144
"""
145145
path = find_root(search_from, indicator)
146-
set_root(path, pythonpath, cwd, project_root_env_var, dotenv)
146+
set_root(
147+
path=path,
148+
project_root_env_var=project_root_env_var,
149+
dotenv=dotenv,
150+
pythonpath=pythonpath,
151+
cwd=cwd,
152+
)
147153
return path

tests/test_pyrootutils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,12 @@ def test_set_root():
6464

6565
assert "PROJECT_ROOT" not in os.environ
6666

67-
set_root(path, pythonpath=False, cwd=False, project_root_env_var=True, dotenv=False)
67+
set_root(path, project_root_env_var=True, dotenv=False, pythonpath=False, cwd=False)
6868
assert "PROJECT_ROOT" in os.environ
6969
assert os.environ["PROJECT_ROOT"] == str(path)
7070
assert os.getcwd() != str(path)
7171

72-
set_root(path, pythonpath=True, cwd=True, project_root_env_var=True, dotenv=True)
72+
set_root(path, project_root_env_var=True, dotenv=True, pythonpath=True, cwd=True)
7373
assert os.getcwd() == str(path)
7474
assert str(path) in sys.path
7575

0 commit comments

Comments
 (0)