Skip to content

Commit 80b939f

Browse files
authored
Merge pull request #5452 from dojutsu-user/remove-asserts
Remove asserts from code.
2 parents 3699b4e + 82e462f commit 80b939f

File tree

7 files changed

+28
-13
lines changed

7 files changed

+28
-13
lines changed

readthedocs/builds/models.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,8 @@ def commit_name(self):
173173
return self.identifier
174174

175175
# By now we must have handled all special versions.
176-
assert self.slug not in NON_REPOSITORY_VERSIONS
176+
if self.slug in NON_REPOSITORY_VERSIONS:
177+
raise Exception('All special versions must be handled by now.')
177178

178179
if self.type in (BRANCH, TAG):
179180
# If this version is a branch or a tag, the verbose_name will

readthedocs/builds/version_slug.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -196,9 +196,9 @@ def create_slug(self, model_instance):
196196
kwargs[self.attname] = slug
197197
count += 1
198198

199-
assert self.test_pattern.match(slug), (
200-
'Invalid generated slug: {slug}'.format(slug=slug)
201-
)
199+
is_slug_valid = self.test_pattern.match(slug)
200+
if not is_slug_valid:
201+
raise Exception('Invalid generated slug: {slug}'.format(slug=slug))
202202
return slug
203203

204204
def pre_save(self, model_instance, add):

readthedocs/core/utils/__init__.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
from readthedocs.builds.constants import BUILD_STATE_TRIGGERED
2020
from readthedocs.doc_builder.constants import DOCKER_LIMITS
21+
from readthedocs.projects.exceptions import InvalidParamsException
2122

2223
log = logging.getLogger(__name__)
2324

@@ -33,7 +34,8 @@ def broadcast(type, task, args, kwargs=None, callback=None): # pylint: disable=
3334
`callback` should be a task signature that will be run once,
3435
after all of the broadcast tasks have finished running.
3536
"""
36-
assert type in ['web', 'app', 'build']
37+
if type not in ['web', 'app', 'build']:
38+
raise InvalidParamsException('allowed value of `type` are web, app and build.')
3739
if kwargs is None:
3840
kwargs = {}
3941
default_queue = getattr(settings, 'CELERY_DEFAULT_QUEUE', 'celery')

readthedocs/doc_builder/environments.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,8 @@ def __init__(
106106
self.cwd = cwd
107107
self.environment = os.environ.copy()
108108
if environment is not None:
109-
assert 'PATH' not in environment, "PATH can't be set"
109+
if 'PATH' in environment:
110+
raise BuildEnvironmentError('\'PATH\' can\'t be set.')
110111
self.environment.update(environment)
111112

112113
self.combine_output = combine_output
@@ -434,7 +435,8 @@ def run_command_class(
434435
env_path = self.environment.pop('BIN_PATH', None)
435436
if 'bin_path' not in kwargs and env_path:
436437
kwargs['bin_path'] = env_path
437-
assert 'environment' not in kwargs, "environment can't be passed in via commands."
438+
if 'environment' in kwargs:
439+
raise BuildEnvironmentError('environment can\'t be passed in via commands.')
438440
kwargs['environment'] = self.environment
439441

440442
# ``build_env`` is passed as ``kwargs`` when it's called from a

readthedocs/projects/exceptions.py

+7
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,10 @@ class ProjectSpamError(Exception):
6060
This error is not raised to users, we use this for banning users in the
6161
background.
6262
"""
63+
64+
65+
class InvalidParamsException(Exception):
66+
67+
"""Error raised when incorrect parameters are passed to a function/class."""
68+
69+
pass

readthedocs/projects/tasks.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@
6767
from readthedocs.worker import app
6868

6969
from .constants import LOG_TEMPLATE
70-
from .exceptions import ProjectConfigurationError, RepositoryError
70+
from .exceptions import ProjectConfigurationError, RepositoryError, InvalidParamsException
7171
from .models import Domain, HTMLFile, ImportedFile, Project
7272
from .signals import (
7373
after_build,
@@ -101,7 +101,8 @@ def get_version(project=None, version_pk=None):
101101
:returns: a data-complete version object
102102
:rtype: builds.models.APIVersion
103103
"""
104-
assert (project or version_pk), 'project or version_pk is needed'
104+
if not (project or version_pk):
105+
raise InvalidParamsException('project or version_pk is needed')
105106
if version_pk:
106107
version_data = api_v2.version(version_pk).get()
107108
else:

readthedocs/search/tasks.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from django_elasticsearch_dsl.registries import registry
55

66
from readthedocs.worker import app
7+
from readthedocs.projects.exceptions import InvalidParamsException
78
from .utils import _get_index, _get_document
89

910
log = logging.getLogger(__name__)
@@ -14,10 +15,11 @@ def index_objects_to_es(
1415
app_label, model_name, document_class, index_name=None, chunk=None, objects_id=None
1516
):
1617

17-
assert not (chunk and objects_id), \
18-
"You can not pass both chunk and objects_id"
19-
assert (chunk or objects_id), \
20-
"You must pass a chunk or objects_id"
18+
if chunk and objects_id:
19+
raise InvalidParamsException('You can not pass both chunk and objects_id.')
20+
21+
if not (chunk or objects_id):
22+
raise InvalidParamsException('You must pass a chunk or objects_id.')
2123

2224
model = apps.get_model(app_label, model_name)
2325
document = _get_document(model=model, document_class=document_class)

0 commit comments

Comments
 (0)