Skip to content

Commit 9eeb37f

Browse files
committed
Clean up None defaults for lists
A sensible default for a list property is an empty list. This change simplifies existing code because we no longer need to turn a None into [] when using an add() method for the first time, and checks in get()s are simpler.
1 parent 25309a2 commit 9eeb37f

2 files changed

Lines changed: 26 additions & 52 deletions

File tree

sendgrid/helpers/mail/mail.py

Lines changed: 14 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@ def __init__(
1616
self._mail_settings = None
1717
self._tracking_settings = None
1818
self._reply_to = None
19-
self._personalizations = None
20-
self._contents = None
21-
self._attachments = None
22-
self._sections = None
23-
self._headers = None
24-
self._categories = None
25-
self._custom_args = None
19+
self._personalizations = []
20+
self._contents = []
21+
self._attachments = []
22+
self._sections = []
23+
self._headers = []
24+
self._categories = []
25+
self._custom_args = []
2626

2727
# Minimum required to send an email
2828
if from_email and subject and to_email and content:
@@ -46,38 +46,38 @@ def get(self):
4646
if self.subject is not None:
4747
mail["subject"] = self.subject
4848

49-
if self.personalizations is not None:
49+
if self.personalizations:
5050
mail["personalizations"] = [
5151
personalization.get()
5252
for personalization in self.personalizations
5353
]
5454

55-
if self.contents is not None:
55+
if self.contents:
5656
mail["content"] = [ob.get() for ob in self.contents]
5757

58-
if self.attachments is not None:
58+
if self.attachments:
5959
mail["attachments"] = [ob.get() for ob in self.attachments]
6060

6161
if self.template_id is not None:
6262
mail["template_id"] = self.template_id
6363

64-
if self.sections is not None:
64+
if self.sections:
6565
sections = {}
6666
for key in self.sections:
6767
sections.update(key.get())
6868
mail["sections"] = sections
6969

70-
if self.headers is not None:
70+
if self.headers:
7171
headers = {}
7272
for key in self.headers:
7373
headers.update(key.get())
7474
mail["headers"] = headers
7575

76-
if self.categories is not None:
76+
if self.categories:
7777
mail["categories"] = [category.get() for category in
7878
self.categories]
7979

80-
if self.custom_args is not None:
80+
if self.custom_args:
8181
custom_args = {}
8282
for key in self.custom_args:
8383
custom_args.update(key.get())
@@ -190,44 +190,34 @@ def personalizations(self):
190190
return self._personalizations
191191

192192
def add_personalization(self, personalizations):
193-
if self._personalizations is None:
194-
self._personalizations = []
195193
self._personalizations.append(personalizations)
196194

197195
@property
198196
def contents(self):
199197
return self._contents
200198

201199
def add_content(self, content):
202-
if self._contents is None:
203-
self._contents = []
204200
self._contents.append(content)
205201

206202
@property
207203
def attachments(self):
208204
return self._attachments
209205

210206
def add_attachment(self, attachment):
211-
if self._attachments is None:
212-
self._attachments = []
213207
self._attachments.append(attachment)
214208

215209
@property
216210
def sections(self):
217211
return self._sections
218212

219213
def add_section(self, section):
220-
if self._sections is None:
221-
self._sections = []
222214
self._sections.append(section)
223215

224216
@property
225217
def headers(self):
226218
return self._headers
227219

228220
def add_header(self, header):
229-
if self._headers is None:
230-
self._headers = []
231221
if isinstance(header, dict):
232222
(k, v) = list(header.items())[0]
233223
self._headers.append(Header(k, v))
@@ -239,15 +229,11 @@ def categories(self):
239229
return self._categories
240230

241231
def add_category(self, category):
242-
if self._categories is None:
243-
self._categories = []
244232
self._categories.append(category)
245233

246234
@property
247235
def custom_args(self):
248236
return self._custom_args
249237

250238
def add_custom_arg(self, custom_arg):
251-
if self._custom_args is None:
252-
self._custom_args = []
253239
self._custom_args.append(custom_arg)

sendgrid/helpers/mail/personalization.py

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
class Personalization(object):
22

33
def __init__(self):
4-
self._tos = None
5-
self._ccs = None
6-
self._bccs = None
4+
self._tos = []
5+
self._ccs = []
6+
self._bccs = []
77
self._subject = None
8-
self._headers = None
9-
self._substitutions = None
10-
self._custom_args = None
8+
self._headers = []
9+
self._substitutions = []
10+
self._custom_args = []
1111
self._send_at = None
1212

1313
@property
@@ -19,8 +19,6 @@ def tos(self, value):
1919
self._tos = value
2020

2121
def add_to(self, email):
22-
if self._tos is None:
23-
self._tos = []
2422
self._tos.append(email.get())
2523

2624
@property
@@ -32,8 +30,6 @@ def ccs(self, value):
3230
self._ccs = value
3331

3432
def add_cc(self, email):
35-
if self._ccs is None:
36-
self._ccs = []
3733
self._ccs.append(email.get())
3834

3935
@property
@@ -45,8 +41,6 @@ def bccs(self, value):
4541
self._bccs = value
4642

4743
def add_bcc(self, email):
48-
if self._bccs is None:
49-
self._bccs = []
5044
self._bccs.append(email.get())
5145

5246
@property
@@ -66,8 +60,6 @@ def headers(self, value):
6660
self._headers = value
6761

6862
def add_header(self, header):
69-
if self._headers is None:
70-
self._headers = []
7163
self._headers.append(header.get())
7264

7365
@property
@@ -79,8 +71,6 @@ def substitutions(self, value):
7971
self.substitutions = value
8072

8173
def add_substitution(self, substitution):
82-
if self._substitutions is None:
83-
self._substitutions = []
8474
self._substitutions.append(substitution.get())
8575

8676
@property
@@ -92,8 +82,6 @@ def custom_args(self, value):
9282
self._custom_args = value
9383

9484
def add_custom_arg(self, custom_arg):
95-
if self._custom_args is None:
96-
self._custom_args = []
9785
self._custom_args.append(custom_arg.get())
9886

9987
@property
@@ -106,31 +94,31 @@ def send_at(self, value):
10694

10795
def get(self):
10896
personalization = {}
109-
if self.tos is not None:
97+
if self.tos:
11098
personalization["to"] = self.tos
11199

112-
if self.ccs is not None:
100+
if self.ccs:
113101
personalization["cc"] = self.ccs
114102

115-
if self.bccs is not None:
103+
if self.bccs:
116104
personalization["bcc"] = self.bccs
117105

118106
if self.subject is not None:
119107
personalization["subject"] = self.subject
120108

121-
if self.headers is not None:
109+
if self.headers:
122110
headers = {}
123111
for key in self.headers:
124112
headers.update(key)
125113
personalization["headers"] = headers
126114

127-
if self.substitutions is not None:
115+
if self.substitutions:
128116
substitutions = {}
129117
for key in self.substitutions:
130118
substitutions.update(key)
131119
personalization["substitutions"] = substitutions
132120

133-
if self.custom_args is not None:
121+
if self.custom_args:
134122
custom_args = {}
135123
for key in self.custom_args:
136124
custom_args.update(key)

0 commit comments

Comments
 (0)