Skip to content

Commit ea2e7e8

Browse files
committed
Python new syntax fixes
1 parent 02f39d0 commit ea2e7e8

File tree

6 files changed

+41
-51
lines changed

6 files changed

+41
-51
lines changed

.pylintrc

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ max-statements = 95
1919
disable=
2020
I,
2121
# W
22-
broad-exception-caught,
2322
deprecated-module,
2423
fixme,
2524
keyword-arg-before-vararg,
@@ -37,8 +36,5 @@ disable=
3736
unidiomatic-typecheck,
3837
# R
3938
duplicate-code,
40-
no-else-raise,
4139
no-else-return,
42-
super-with-arguments,
4340
too-few-public-methods,
44-
useless-object-inheritance,

tests/test_change_tz.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
from vobject.change_tz import change_tz
66

77

8-
class StubCal(object):
9-
class StubEvent(object):
10-
class Node(object):
8+
class StubCal:
9+
class StubEvent:
10+
class Node:
1111
def __init__(self, value):
1212
self.value = value
1313

vobject/base.py

Lines changed: 29 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ def to_basestring(s):
6969
# --------------------------------- Main classes -------------------------------
7070

7171

72-
class VBase(object):
72+
class VBase:
7373
"""
7474
Base class for ContentLine and Component.
7575
@@ -88,7 +88,7 @@ class VBase(object):
8888
"""
8989

9090
def __init__(self, group=None, *args, **kwds):
91-
super(VBase, self).__init__(*args, **kwds)
91+
super().__init__(*args, **kwds)
9292
self.name = None
9393
self.group = group
9494
self.behavior = None
@@ -176,11 +176,11 @@ def transformToNative(self):
176176
if lineNumber is not None:
177177
e.lineNumber = lineNumber
178178
raise
179-
else:
180-
msg = "In transformToNative, unhandled exception on line {0}: {1}: {2}"
181-
msg = msg.format(lineNumber, sys.exc_info()[0], sys.exc_info()[1])
182-
msg = msg + " (" + str(self_orig) + ")"
183-
raise ParseError(msg, lineNumber)
179+
180+
msg = "In transformToNative, unhandled exception on line {0}: {1}: {2}"
181+
msg = msg.format(lineNumber, sys.exc_info()[0], sys.exc_info()[1])
182+
msg = msg + " (" + str(self_orig) + ")"
183+
raise ParseError(msg, lineNumber)
184184

185185
def transformFromNative(self):
186186
"""
@@ -205,10 +205,10 @@ def transformFromNative(self):
205205
if lineNumber is not None:
206206
e.lineNumber = lineNumber
207207
raise
208-
else:
209-
msg = "In transformFromNative, unhandled exception on line {0} {1}: {2}"
210-
msg = msg.format(lineNumber, sys.exc_info()[0], sys.exc_info()[1])
211-
raise NativeError(msg, lineNumber)
208+
209+
msg = "In transformFromNative, unhandled exception on line {0} {1}: {2}"
210+
msg = msg.format(lineNumber, sys.exc_info()[0], sys.exc_info()[1])
211+
raise NativeError(msg, lineNumber)
212212
else:
213213
return self
214214

@@ -282,7 +282,7 @@ def __init__(self, name, params, value, group=None, encoded=False, isNative=Fals
282282
283283
Group is used as a positional argument to match parseLine's return
284284
"""
285-
super(ContentLine, self).__init__(group, *args, **kwds)
285+
super().__init__(group, *args, **kwds)
286286

287287
self.name = name.upper()
288288
self.encoded = encoded
@@ -330,7 +330,7 @@ def duplicate(cls, copyit):
330330
return newcopy
331331

332332
def copy(self, copyit):
333-
super(ContentLine, self).copy(copyit)
333+
super().copy(copyit)
334334
self.name = copyit.name
335335
self.value = copy.copy(copyit.value)
336336
self.encoded = self.encoded
@@ -343,7 +343,7 @@ def copy(self, copyit):
343343
def __eq__(self, other):
344344
try:
345345
return (self.name == other.name) and (self.params == other.params) and (self.value == other.value)
346-
except Exception:
346+
except AttributeError:
347347
return False
348348

349349
def __getattr__(self, name):
@@ -449,7 +449,7 @@ class Component(VBase):
449449
"""
450450

451451
def __init__(self, name=None, *args, **kwds):
452-
super(Component, self).__init__(*args, **kwds)
452+
super().__init__(*args, **kwds)
453453
self.contents = {}
454454
if name:
455455
self.name = name.upper()
@@ -467,7 +467,7 @@ def duplicate(cls, copyit):
467467
return newcopy
468468

469469
def copy(self, copyit):
470-
super(Component, self).copy(copyit)
470+
super().copy(copyit)
471471

472472
# deep copy of contents
473473
self.contents = {}
@@ -627,7 +627,7 @@ def lines(self):
627627
def sortChildKeys(self):
628628
try:
629629
first = [s for s in self.behavior.sortFirst if s in self.contents]
630-
except Exception:
630+
except AttributeError:
631631
first = []
632632
return first + sorted(k for k in self.contents if k not in first)
633633

@@ -949,13 +949,18 @@ def foldOneLine(outbuf, input_, lineLength=75):
949949
950950
TO-DO: This all seems odd. Is it still needed, especially in python3?
951951
"""
952-
if len(input_) < lineLength:
953-
# Optimize for unfolded line case
952+
953+
def write_to_outbuf(text):
954+
# TODO: remove py2
954955
try:
955-
outbuf.write(bytes(input_, "UTF-8"))
956-
except Exception:
956+
outbuf.write(bytes(text, "UTF-8"))
957+
except Exception: # pylint: disable=broad-exception-caught
957958
# fall back on py2 syntax
958-
outbuf.write(input_)
959+
outbuf.write(text)
960+
961+
if len(input_) < lineLength:
962+
# Optimize for unfolded line case
963+
write_to_outbuf(text=input_)
959964

960965
else:
961966
# Look for valid utf8 range and write that out
@@ -968,12 +973,7 @@ def foldOneLine(outbuf, input_, lineLength=75):
968973
s = decoded[start] # take one char
969974
size = len(to_basestring(s)) # calculate it's size in bytes
970975
if counter + size > lineLength:
971-
try:
972-
outbuf.write(bytes("\r\n ", "UTF-8"))
973-
except Exception:
974-
# fall back on py2 syntax
975-
outbuf.write("\r\n ")
976-
976+
write_to_outbuf(text="\r\n ")
977977
counter = 1 # one for space
978978

979979
if str is unicode_type:
@@ -985,11 +985,7 @@ def foldOneLine(outbuf, input_, lineLength=75):
985985
written += size
986986
counter += size
987987
start += 1
988-
try:
989-
outbuf.write(bytes("\r\n", "UTF-8"))
990-
except Exception:
991-
# fall back on py2 syntax
992-
outbuf.write("\r\n")
988+
write_to_outbuf(text="\r\n")
993989

994990

995991
def defaultSerialize(obj, buf, lineLength):

vobject/behavior.py

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

33

44
# ------------------------ Abstract class for behavior --------------------------
5-
class Behavior(object):
5+
class Behavior:
66
"""
77
Behavior (validation, encoding, and transformations) for vobjects.
88

vobject/icalendar.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ def __init__(self, tzinfo=None, *args, **kwds):
119119
"""
120120
Accept an existing Component or a tzinfo class.
121121
"""
122-
super(TimezoneComponent, self).__init__(*args, **kwds)
122+
super().__init__(*args, **kwds)
123123
self.isNative = True
124124
# hack to make sure a behavior is assigned
125125
if self.behavior is None:
@@ -404,7 +404,7 @@ class RecurringComponent(Component):
404404
"""
405405

406406
def __init__(self, *args, **kwds):
407-
super(RecurringComponent, self).__init__(*args, **kwds)
407+
super().__init__(*args, **kwds)
408408

409409
self.isNative = True
410410

@@ -671,7 +671,7 @@ def __setattr__(self, name, value):
671671
if name == "rruleset":
672672
self.setrruleset(value)
673673
else:
674-
super(RecurringComponent, self).__setattr__(name, value)
674+
super().__setattr__(name, value)
675675

676676

677677
class TextBehavior(behavior.Behavior):
@@ -1052,7 +1052,7 @@ def serialize(cls, obj, buf, lineLength, validate=True, *args, **kwargs):
10521052
first_components = [
10531053
s for s in cls.sortFirst if s in obj.contents and isinstance(obj.contents[s][0], Component)
10541054
]
1055-
except Exception:
1055+
except VObjectError:
10561056
first_props = first_components = []
10571057
# first_components = []
10581058

@@ -1849,8 +1849,7 @@ def escapableChar(c):
18491849
def error(msg):
18501850
if strict:
18511851
raise ParseError(msg)
1852-
else:
1853-
logging.error(msg)
1852+
logging.error(msg)
18541853

18551854
# vars which control state machine
18561855
charIterator = enumerate(s)
@@ -1925,8 +1924,7 @@ def makeTimedelta(sign, week, day, hour, minute, sec):
19251924
def error(msg):
19261925
if strict:
19271926
raise ParseError(msg)
1928-
else:
1929-
raise ParseError(msg)
1927+
raise ParseError(msg)
19301928

19311929
# vars which control state machine
19321930
charIterator = enumerate(s)

vobject/vcard.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
# ------------------------ vCard structs ---------------------------------------
1010

1111

12-
class Name(object):
12+
class Name:
1313
def __init__(self, family="", given="", additional="", prefix="", suffix=""):
1414
"""
1515
Each name attribute can be a string or a list of strings.
@@ -50,7 +50,7 @@ def __eq__(self, other):
5050
return False
5151

5252

53-
class Address(object):
53+
class Address:
5454
def __init__(self, street="", city="", region="", code="", country="", box="", extended=""):
5555
"""
5656
Each name attribute can be a string or a list of strings.

0 commit comments

Comments
 (0)