Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit c300922

Browse filesBrowse files
authored
Deprecate js override bits
They were a feature for testing whose need was removed in 2015 (ua-parser/uap-core#58). They are *entirely* unnecessary, it is possible a user somewhere is leveraging them for some reason. So remove the override bits from the docstrings, and have them trigger a `DeprecationWarning`. Also: - add tests that they do trigger - remove support for them from the yaml testing functions - update the pytest invocations to raise on all warnings - while at it remove the apparently dead makePGTSComparisonYAML test utility method (?) - and add pypy to the envlist Note: `simplefilter` has to be updated to `"always"` for 2.7's dumb ass: the new tests triggers the same warning multiple times and of course `"default"` only yields them once. Closes #119
1 parent f9d60ec commit c300922
Copy full SHA for c300922

File tree

4 files changed

+39
-54
lines changed
Filter options

4 files changed

+39
-54
lines changed

‎.github/workflows/ci.yml

Copy file name to clipboardExpand all lines: .github/workflows/ci.yml
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ jobs:
5454
- name: install package in environment
5555
run: python setup.py develop
5656
- name: run tests
57-
run: pytest -v
57+
run: pytest -v -Werror
5858
- name: run doctests
5959
# pprint formatting was changed a lot in 3.5
6060
if: ${{ matrix.python-version != '2.7' }}

‎tox.ini

Copy file name to clipboardExpand all lines: tox.ini
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
[tox]
2-
envlist = py27, py36, py37, py38, py39, py310, docs, flake8, black
2+
envlist = py27, py36, py37, py38, py39, py310, pypy3.8, docs, flake8, black
33
skipsdist = True
44

55
[testenv]
66
usedevelop = True
77
deps = -rrequirements_dev.txt
88
commands =
9-
pytest {posargs}
9+
pytest -Werror {posargs}
1010
python -mdoctest README.rst
1111

1212
[testenv:py27]

‎ua_parser/user_agent_parser.py

Copy file name to clipboardExpand all lines: ua_parser/user_agent_parser.py
+18-4Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,6 @@ def Parse(user_agent_string, **jsParseBits):
244244
"""Parse all the things
245245
Args:
246246
user_agent_string: the full user agent string
247-
jsParseBits: javascript override bits
248247
Returns:
249248
A dictionary containing all parsed bits
250249
"""
@@ -268,14 +267,19 @@ def ParseUserAgent(user_agent_string, **jsParseBits):
268267
"""Parses the user-agent string for user agent (browser) info.
269268
Args:
270269
user_agent_string: The full user-agent string.
271-
jsParseBits: javascript override bits.
272270
Returns:
273271
A dictionary containing parsed bits.
274272
"""
275273
return _cached(user_agent_string, jsParseBits, "user_agent", _ParseUserAgent)
276274

277275

278276
def _ParseUserAgent(user_agent_string, jsParseBits):
277+
if jsParseBits:
278+
warnings.warn(
279+
"javascript overrides are deprecated and will be removed next release",
280+
category=DeprecationWarning,
281+
stacklevel=2,
282+
)
279283
if (
280284
"js_user_agent_family" in jsParseBits
281285
and jsParseBits["js_user_agent_family"] != ""
@@ -318,14 +322,19 @@ def ParseOS(user_agent_string, **jsParseBits):
318322
"""Parses the user-agent string for operating system info
319323
Args:
320324
user_agent_string: The full user-agent string.
321-
jsParseBits: javascript override bits.
322325
Returns:
323326
A dictionary containing parsed bits.
324327
"""
325328
return _cached(user_agent_string, jsParseBits, "os", _ParseOS)
326329

327330

328331
def _ParseOS(user_agent_string, jsParseBits):
332+
if jsParseBits:
333+
warnings.warn(
334+
"javascript overrides are deprecated and will be removed next release",
335+
category=DeprecationWarning,
336+
stacklevel=2,
337+
)
329338
for osParser in OS_PARSERS:
330339
os, os_v1, os_v2, os_v3, os_v4 = osParser.Parse(user_agent_string)
331340
if os:
@@ -344,14 +353,19 @@ def ParseDevice(user_agent_string, **jsParseBits):
344353
"""Parses the user-agent string for device info.
345354
Args:
346355
user_agent_string: The full user-agent string.
347-
ua_family: The parsed user agent family name.
348356
Returns:
349357
A dictionary containing parsed bits.
350358
"""
351359
return _cached(user_agent_string, jsParseBits, "device", _ParseDevice)
352360

353361

354362
def _ParseDevice(user_agent_string, jsParseBits):
363+
if jsParseBits:
364+
warnings.warn(
365+
"javascript overrides are deprecated and will be removed next release",
366+
category=DeprecationWarning,
367+
stacklevel=2,
368+
)
355369
for deviceParser in DEVICE_PARSERS:
356370
device, brand, model = deviceParser.Parse(user_agent_string)
357371
if device:

‎ua_parser/user_agent_parser_test.py

Copy file name to clipboardExpand all lines: ua_parser/user_agent_parser_test.py
+18-47Lines changed: 18 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
#!/usr/bin/python2.5
2-
#
31
# Copyright 2008 Google Inc.
42
#
53
# Licensed under the Apache License, Version 2.0 (the 'License')
@@ -121,38 +119,6 @@ def testParseAll(self):
121119
),
122120
)
123121

124-
# Make a YAML file for manual comparsion with pgts_browser_list-orig.yaml
125-
def makePGTSComparisonYAML(self):
126-
import codecs
127-
128-
outfile = codecs.open("outfile.yaml", "w", "utf-8")
129-
print >> outfile, "test_cases:"
130-
131-
yamlFile = open(os.path.join(TEST_RESOURCES_DIR, "pgts_browser_list.yaml"))
132-
yamlContents = yaml.load(yamlFile, Loader=SafeLoader)
133-
yamlFile.close()
134-
135-
for test_case in yamlContents["test_cases"]:
136-
user_agent_string = test_case["user_agent_string"]
137-
kwds = {}
138-
if "js_ua" in test_case:
139-
kwds = eval(test_case["js_ua"])
140-
141-
(family, major, minor, patch) = user_agent_parser.ParseUserAgent(
142-
user_agent_string, **kwds
143-
)
144-
145-
# Escape any double-quotes in the UA string
146-
user_agent_string = re.sub(r'"', '\\"', user_agent_string)
147-
print >> outfile, ' - user_agent_string: "' + user_agent_string + '"' + "\n" + ' family: "' + family + '"\n' + " major: " + (
148-
"" if (major is None) else "'" + major + "'"
149-
) + "\n" + " minor: " + (
150-
"" if (minor is None) else "'" + minor + "'"
151-
) + "\n" + " patch: " + (
152-
"" if (patch is None) else "'" + patch + "'"
153-
)
154-
outfile.close()
155-
156122
# Run a set of test cases from a YAML file
157123
def runUserAgentTestsFromYAML(self, file_name):
158124
yamlFile = open(os.path.join(TEST_RESOURCES_DIR, file_name))
@@ -162,9 +128,6 @@ def runUserAgentTestsFromYAML(self, file_name):
162128
for test_case in yamlContents["test_cases"]:
163129
# Inputs to Parse()
164130
user_agent_string = test_case["user_agent_string"]
165-
kwds = {}
166-
if "js_ua" in test_case:
167-
kwds = eval(test_case["js_ua"])
168131

169132
# The expected results
170133
expected = {
@@ -175,7 +138,7 @@ def runUserAgentTestsFromYAML(self, file_name):
175138
}
176139

177140
result = {}
178-
result = user_agent_parser.ParseUserAgent(user_agent_string, **kwds)
141+
result = user_agent_parser.ParseUserAgent(user_agent_string)
179142
self.assertEqual(
180143
result,
181144
expected,
@@ -205,9 +168,6 @@ def runOSTestsFromYAML(self, file_name):
205168
for test_case in yamlContents["test_cases"]:
206169
# Inputs to Parse()
207170
user_agent_string = test_case["user_agent_string"]
208-
kwds = {}
209-
if "js_ua" in test_case:
210-
kwds = eval(test_case["js_ua"])
211171

212172
# The expected results
213173
expected = {
@@ -218,7 +178,7 @@ def runOSTestsFromYAML(self, file_name):
218178
"patch_minor": test_case["patch_minor"],
219179
}
220180

221-
result = user_agent_parser.ParseOS(user_agent_string, **kwds)
181+
result = user_agent_parser.ParseOS(user_agent_string)
222182
self.assertEqual(
223183
result,
224184
expected,
@@ -245,9 +205,6 @@ def runDeviceTestsFromYAML(self, file_name):
245205
for test_case in yamlContents["test_cases"]:
246206
# Inputs to Parse()
247207
user_agent_string = test_case["user_agent_string"]
248-
kwds = {}
249-
if "js_ua" in test_case:
250-
kwds = eval(test_case["js_ua"])
251208

252209
# The expected results
253210
expected = {
@@ -256,7 +213,7 @@ def runDeviceTestsFromYAML(self, file_name):
256213
"model": test_case["model"],
257214
}
258215

259-
result = user_agent_parser.ParseDevice(user_agent_string, **kwds)
216+
result = user_agent_parser.ParseDevice(user_agent_string)
260217
self.assertEqual(
261218
result,
262219
expected,
@@ -308,7 +265,7 @@ def setUp(self):
308265
seems to work out of the box.
309266
"""
310267
super(TestDeprecationWarnings, self).setUp()
311-
warnings.simplefilter("default", DeprecationWarning)
268+
warnings.simplefilter("always", DeprecationWarning)
312269

313270
def tearDown(self):
314271
# not ideal as it discards all other warnings updates from the
@@ -329,6 +286,20 @@ def test_printer_deprecation(self):
329286
self.assertEqual(len(ws), 1)
330287
self.assertEqual(ws[0].category, DeprecationWarning)
331288

289+
def test_js_bits_deprecation(self):
290+
for parser, count in [
291+
(user_agent_parser.Parse, 3),
292+
(user_agent_parser.ParseUserAgent, 1),
293+
(user_agent_parser.ParseOS, 1),
294+
(user_agent_parser.ParseDevice, 1),
295+
]:
296+
user_agent_parser._PARSE_CACHE.clear()
297+
with warnings.catch_warnings(record=True) as ws:
298+
parser("some random thing", js_attribute=True)
299+
self.assertEqual(len(ws), count)
300+
for w in ws:
301+
self.assertEqual(w.category, DeprecationWarning)
302+
332303

333304
if __name__ == "__main__":
334305
unittest.main()

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.