Coverage for /home/martinb/.local/share/virtualenvs/camcops/lib/python3.6/site-packages/faker/providers/ssn/en_US/__init__.py : 20%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1from .. import Provider as BaseProvider
4class Provider(BaseProvider):
5 INVALID_SSN_TYPE = 'INVALID_SSN'
6 SSN_TYPE = 'SSN'
7 ITIN_TYPE = 'ITIN'
8 EIN_TYPE = 'EIN'
10 def itin(self):
11 """Generate a random United States Individual Taxpayer Identification Number (ITIN).
13 An United States Individual Taxpayer Identification Number
14 (ITIN) is a tax processing number issued by the Internal
15 Revenue Service. It is a nine-digit number that always begins
16 with the number 9 and has a range of 70-88 in the fourth and
17 fifth digit. Effective April 12, 2011, the range was extended
18 to include 900-70-0000 through 999-88-9999, 900-90-0000
19 through 999-92-9999 and 900-94-0000 through 999-99-9999.
20 https://www.irs.gov/individuals/international-taxpayers/general-itin-information
21 """
23 area = self.random_int(min=900, max=999)
24 serial = self.random_int(min=0, max=9999)
26 # The group number must be between 70 and 99 inclusively but not 89 or 93
27 group = self.random_element([x for x in range(70, 100) if x not in [89, 93]])
29 itin = "{:03d}-{:02d}-{:04d}".format(area, group, serial)
30 return itin
32 def ein(self):
33 """Generate a random United States Employer Identification Number (EIN).
35 An United States An Employer Identification Number (EIN) is
36 also known as a Federal Tax Identification Number, and is
37 used to identify a business entity. EINs follow a format of a
38 two-digit prefix followed by a hyphen and a seven-digit sequence:
39 ##-######
41 https://www.irs.gov/businesses/small-businesses-self-employed/employer-id-numbers
42 """
44 # Only certain EIN Prefix values are assigned:
45 #
46 # https://www.irs.gov/businesses/small-businesses-self-employed/how-eins-are-assigned-and-valid-ein-prefixes
48 ein_prefix_choices = [
49 '01',
50 '02',
51 '03',
52 '04',
53 '05',
54 '06',
55 '10',
56 '11',
57 '12',
58 '13',
59 '14',
60 '15',
61 '16',
62 '20',
63 '21',
64 '22',
65 '23',
66 '24',
67 '25',
68 '26',
69 '27',
70 '30',
71 '31',
72 '32',
73 '33',
74 '34',
75 '35',
76 '36',
77 '37',
78 '38',
79 '39',
80 '40',
81 '41',
82 '42',
83 '43',
84 '44',
85 '45',
86 '46',
87 '47',
88 '48',
89 '50',
90 '51',
91 '52',
92 '53',
93 '54',
94 '55',
95 '56',
96 '57',
97 '58',
98 '59',
99 '60',
100 '61',
101 '62',
102 '63',
103 '64',
104 '65',
105 '66',
106 '67',
107 '68',
108 '71',
109 '72',
110 '73',
111 '74',
112 '75',
113 '76',
114 '77',
115 '80',
116 '81',
117 '82',
118 '83',
119 '84',
120 '85',
121 '86',
122 '87',
123 '88',
124 '90',
125 '91',
126 '92',
127 '93',
128 '94',
129 '95',
130 '98',
131 '99']
133 ein_prefix = self.random_element(ein_prefix_choices)
134 sequence = self.random_int(min=0, max=9999999)
136 ein = "{:s}-{:07d}".format(ein_prefix, sequence)
137 return ein
139 def invalid_ssn(self):
140 """ Generate a random invalid United States Social Security Identification Number (SSN).
142 Invalid SSNs have the following characteristics:
143 Cannot begin with the number 9
144 Cannot begin with 666 in positions 1 - 3
145 Cannot begin with 000 in positions 1 - 3
146 Cannot contain 00 in positions 4 - 5
147 Cannot contain 0000 in positions 6 - 9
149 https://www.ssa.gov/kc/SSAFactSheet--IssuingSSNs.pdf
151 Additionally, return an invalid SSN that is NOT a valid ITIN by excluding certain ITIN related "group" values
152 """
153 itin_group_numbers = [
154 70,
155 71,
156 72,
157 73,
158 74,
159 75,
160 76,
161 77,
162 78,
163 79,
164 80,
165 81,
166 82,
167 83,
168 84,
169 85,
170 86,
171 87,
172 88,
173 90,
174 91,
175 92,
176 94,
177 95,
178 96,
179 97,
180 98,
181 99]
182 area = self.random_int(min=0, max=999)
183 if area < 900 and area not in {666, 0}:
184 random_group_or_serial = self.random_int(min=1, max=1000)
185 if random_group_or_serial <= 500:
186 group = 0
187 serial = self.random_int(0, 9999)
188 else:
189 group = self.random_int(0, 99)
190 serial = 0
191 elif area in {666, 0}:
192 group = self.random_int(0, 99)
193 serial = self.random_int(0, 9999)
194 else:
195 group = self.random_element([x for x in range(0, 100) if x not in itin_group_numbers])
196 serial = self.random_int(0, 9999)
198 invalid_ssn = "{:03d}-{:02d}-{:04d}".format(area, group, serial)
199 return invalid_ssn
201 def ssn(self, taxpayer_identification_number_type=SSN_TYPE):
202 """ Generate a random United States Taxpayer Identification Number of the specified type.
204 If no type is specified, a US SSN is returned.
205 """
207 if taxpayer_identification_number_type == self.ITIN_TYPE:
208 return self.itin()
209 elif taxpayer_identification_number_type == self.EIN_TYPE:
210 return self.ein()
211 elif taxpayer_identification_number_type == self.INVALID_SSN_TYPE:
212 return self.invalid_ssn()
213 elif taxpayer_identification_number_type == self.SSN_TYPE:
215 # Certain numbers are invalid for United States Social Security
216 # Numbers. The area (first 3 digits) cannot be 666 or 900-999.
217 # The group number (middle digits) cannot be 00. The serial
218 # (last 4 digits) cannot be 0000.
220 area = self.random_int(min=1, max=899)
221 if area == 666:
222 area += 1
223 group = self.random_int(1, 99)
224 serial = self.random_int(1, 9999)
226 ssn = "{:03d}-{:02d}-{:04d}".format(area, group, serial)
227 return ssn
229 else:
230 raise ValueError("taxpayer_identification_number_type must be one of 'SSN', 'EIN', 'ITIN',"
231 " or 'INVALID_SSN'.")