Skip to content

Commit 48f97c6

Browse files
chore: fix/refactor code near unneeded global declarations (#8765)
* refactor: unused vars in draft.py * refactor: unwrap/simplify random_faker() prep * chore: types/globals in test_runner.py * chore: drop unneeded globals
1 parent 9957cf1 commit 48f97c6

File tree

5 files changed

+30
-40
lines changed

5 files changed

+30
-40
lines changed

ietf/doc/templatetags/wg_menu.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,6 @@
6262

6363
@register.simple_tag
6464
def wg_menu(flavor):
65-
global parents
66-
6765
for p in parents:
6866
p.short_name = parent_short_names.get(p.acronym) or p.name
6967
if p.short_name.endswith(" Area"):

ietf/person/factories.py

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,20 +26,22 @@
2626

2727
fake = faker.Factory.create()
2828

29-
def setup():
30-
global acceptable_fakers
31-
# The transliteration of some Arabic and Devanagari names introduces
32-
# non-alphabetic characters that don't work with the draft author
33-
# extraction code, and also don't seem to match the way people with Arabic
34-
# names romanize Arabic names. Exclude those locales from name generation
35-
# in order to avoid test failures.
36-
locales = set( [ l for l in faker.config.AVAILABLE_LOCALES if not (l.startswith('ar_') or l.startswith('sg_') or l=='fr_QC') ] )
37-
acceptable_fakers = [faker.Faker(locale) for locale in locales]
38-
setup()
29+
# The transliteration of some Arabic and Devanagari names introduces
30+
# non-alphabetic characters that don't work with the draft author
31+
# extraction code, and also don't seem to match the way people with Arabic
32+
# names romanize Arabic names. Exclude those locales from name generation
33+
# in order to avoid test failures.
34+
_acceptable_fakers = [
35+
faker.Faker(locale)
36+
for locale in set(faker.config.AVAILABLE_LOCALES)
37+
if not (locale.startswith('ar_') or locale.startswith('sg_') or locale == 'fr_QC')
38+
]
39+
3940

4041
def random_faker():
41-
global acceptable_fakers
42-
return random.sample(acceptable_fakers, 1)[0]
42+
"""Helper to get a random faker acceptable for User names"""
43+
return random.sample(_acceptable_fakers, 1)[0]
44+
4345

4446
class UserFactory(factory.django.DjangoModelFactory):
4547
class Meta:

ietf/utils/draft.py

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@
6565
opt_debug = False
6666
opt_timestamp = False
6767
opt_trace = False
68-
opt_authorinfo = False
6968
opt_attributes = False
7069
# Don't forget to add the option variable to the globals list in _main below
7170

@@ -1332,8 +1331,6 @@ def getmeta(fn):
13321331

13331332
# ----------------------------------------------------------------------
13341333
def _output(docname, fields, outfile=sys.stdout):
1335-
global company_domain
1336-
13371334
if opt_attributes:
13381335
def outputkey(key, fields):
13391336
field = fields[key]
@@ -1373,9 +1370,8 @@ def _printmeta(fn, outfile=sys.stdout):
13731370
# Main
13741371
# ----------------------------------------------------------------------
13751372

1376-
company_domain = {} # type: Dict[str, str]
13771373
def _main(outfile=sys.stdout):
1378-
global opt_debug, opt_timestamp, opt_trace, opt_authorinfo, files, company_domain, opt_attributes
1374+
global opt_debug, opt_timestamp, opt_trace, files, opt_attributes
13791375
# set default values, if any
13801376
# ----------------------------------------------------------------------
13811377
# Option processing
@@ -1423,8 +1419,6 @@ def _main(outfile=sys.stdout):
14231419
elif opt in ["-T", "--trace"]: # Emit trace information while working
14241420
opt_trace = True
14251421

1426-
company_domain = {}
1427-
14281422
if not files:
14291423
files = [ "-" ]
14301424

ietf/utils/patch.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,7 @@ def createLock(self):
8787
debugmode = False
8888

8989
def setdebug():
90-
global debugmode, streamhandler
91-
90+
global debugmode
9291
debugmode = True
9392
loglevel = logging.DEBUG
9493
logformat = "%(levelname)8s %(message)s"

ietf/utils/test_runner.py

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,10 @@
5353
import factory.random
5454
import urllib3
5555
import warnings
56-
from urllib.parse import urlencode
5756

5857
from fnmatch import fnmatch
58+
from typing import Callable, Optional
59+
from urllib.parse import urlencode
5960

6061
from coverage.report import Reporter
6162
from coverage.results import Numbers
@@ -90,11 +91,11 @@
9091
from mypy_boto3_s3.service_resource import Bucket
9192

9293

93-
loaded_templates = set()
94-
visited_urls = set()
95-
test_database_name = None
96-
old_destroy = None
97-
old_create = None
94+
loaded_templates: set[str] = set()
95+
visited_urls: set[str] = set()
96+
test_database_name: Optional[str] = None
97+
old_destroy: Optional[Callable] = None
98+
old_create: Optional[Callable] = None
9899

99100
template_coverage_collection = None
100101
code_coverage_collection = None
@@ -230,10 +231,12 @@ def load_and_run_fixtures(verbosity):
230231
fn()
231232

232233
def safe_create_test_db(self, verbosity, *args, **kwargs):
233-
global test_database_name, old_create
234+
if old_create is None:
235+
raise RuntimeError("old_create has not been set, cannot proceed")
234236
keepdb = kwargs.get('keepdb', False)
235237
if not keepdb:
236238
print(" Creating test database...")
239+
global test_database_name
237240
test_database_name = old_create(self, 0, *args, **kwargs)
238241

239242
if settings.GLOBAL_TEST_FIXTURES:
@@ -243,8 +246,9 @@ def safe_create_test_db(self, verbosity, *args, **kwargs):
243246
return test_database_name
244247

245248
def safe_destroy_test_db(*args, **kwargs):
249+
if old_destroy is None:
250+
raise RuntimeError("old_destroy has not been set, cannot proceed")
246251
sys.stdout.write('\n')
247-
global test_database_name, old_destroy
248252
keepdb = kwargs.get('keepdb', False)
249253
if not keepdb:
250254
if settings.DATABASES["default"]["NAME"] != test_database_name:
@@ -358,15 +362,13 @@ class TemplateCoverageLoader(BaseLoader):
358362
is_usable = True
359363

360364
def get_template(self, template_name, skip=None):
361-
global template_coverage_collection, loaded_templates
362-
if template_coverage_collection == True:
365+
if template_coverage_collection:
363366
loaded_templates.add(str(template_name))
364367
raise TemplateDoesNotExist(template_name)
365368

366369
def record_urls_middleware(get_response):
367370
def record_urls(request):
368-
global url_coverage_collection, visited_urls
369-
if url_coverage_collection == True:
371+
if url_coverage_collection:
370372
visited_urls.add(request.path)
371373
return get_response(request)
372374
return record_urls
@@ -532,7 +534,6 @@ def report_test_result(self, test):
532534
( test, test_coverage*100, latest_coverage_version, master_coverage*100, ))
533535

534536
def template_coverage_test(self):
535-
global loaded_templates
536537
if self.runner.check_coverage:
537538
apps = [ app.split('.')[-1] for app in self.runner.test_apps ]
538539
all = get_template_paths(apps)
@@ -760,7 +761,6 @@ def __init__(
760761
self.show_logging = show_logging
761762
self.rerun = rerun
762763
self.test_labels = None
763-
global validation_settings
764764
validation_settings["validate_html"] = self if validate_html else None
765765
validation_settings["validate_html_harder"] = self if validate_html and validate_html_harder else None
766766
validation_settings["show_logging"] = show_logging
@@ -783,9 +783,6 @@ def __init__(
783783
self.blobstoremanager = TestBlobstoreManager() if manage_blobstore else None
784784

785785
def setup_test_environment(self, **kwargs):
786-
global template_coverage_collection
787-
global url_coverage_collection
788-
789786
ietf.utils.mail.test_mode = True
790787
ietf.utils.mail.SMTP_ADDR['ip4'] = '127.0.0.1'
791788
ietf.utils.mail.SMTP_ADDR['port'] = 2025

0 commit comments

Comments
 (0)