@@ -140,6 +140,8 @@ def seq_lrepr(
140
140
return f"{ start } { seq_lrepr } { end } "
141
141
142
142
143
+ # pylint: disable=unused-argument
144
+ @singledispatch
143
145
def lrepr ( # pylint: disable=too-many-arguments
144
146
o : Any ,
145
147
human_readable : bool = False ,
@@ -170,29 +172,11 @@ def lrepr( # pylint: disable=too-many-arguments
170
172
runtime to the basilisp.core dynamic variables which correspond to each
171
173
of the keyword arguments to this function. To use a version of lrepr
172
174
which does capture those values, call basilisp.lang.runtime.lrepr directly."""
173
- if isinstance (o , LispObject ):
174
- return o ._lrepr (
175
- human_readable = human_readable ,
176
- print_dup = print_dup ,
177
- print_length = print_length ,
178
- print_level = print_level ,
179
- print_meta = print_meta ,
180
- print_readably = print_readably ,
181
- )
182
- else : # pragma: no cover
183
- return _lrepr_fallback (
184
- o ,
185
- human_readable = human_readable ,
186
- print_dup = print_dup ,
187
- print_length = print_length ,
188
- print_level = print_level ,
189
- print_meta = print_meta ,
190
- print_readably = print_readably ,
191
- )
175
+ return repr (o )
192
176
193
177
194
- @singledispatch
195
- def _lrepr_fallback ( # pylint: disable=too-many-arguments
178
+ @lrepr . register ( LispObject )
179
+ def _lrepr_lisp_obj ( # pylint: disable=too-many-arguments
196
180
o : Any ,
197
181
human_readable : bool = False ,
198
182
print_dup : bool = PRINT_DUP ,
@@ -201,62 +185,27 @@ def _lrepr_fallback( # pylint: disable=too-many-arguments
201
185
print_meta : bool = PRINT_META ,
202
186
print_readably : bool = PRINT_READABLY ,
203
187
) -> str : # pragma: no cover
204
- """Fallback function for lrepr for subclasses of standard types.
205
-
206
- The singledispatch used for standard lrepr dispatches using an exact
207
- type match on the first argument, so we will only hit this function
208
- for subclasses of common Python types like strings or lists."""
209
- kwargs = {
210
- "human_readable" : human_readable ,
211
- "print_dup" : print_dup ,
212
- "print_length" : print_length ,
213
- "print_level" : print_level ,
214
- "print_meta" : print_meta ,
215
- "print_readably" : print_readably ,
216
- }
217
- if isinstance (o , bool ):
218
- return _lrepr_bool (o )
219
- elif o is None :
220
- return _lrepr_nil (o )
221
- elif isinstance (o , str ):
222
- return _lrepr_str (
223
- o , human_readable = human_readable , print_readably = print_readably
224
- )
225
- elif isinstance (o , dict ):
226
- return _lrepr_py_dict (o , ** kwargs )
227
- elif isinstance (o , list ):
228
- return _lrepr_py_list (o , ** kwargs )
229
- elif isinstance (o , set ):
230
- return _lrepr_py_set (o , ** kwargs )
231
- elif isinstance (o , tuple ):
232
- return _lrepr_py_tuple (o , ** kwargs )
233
- elif isinstance (o , complex ):
234
- return _lrepr_complex (o )
235
- elif isinstance (o , datetime .datetime ):
236
- return _lrepr_datetime (o )
237
- elif isinstance (o , Decimal ):
238
- return _lrepr_decimal (o , print_dup = print_dup )
239
- elif isinstance (o , Fraction ):
240
- return _lrepr_fraction (o )
241
- elif isinstance (o , Pattern ):
242
- return _lrepr_pattern (o )
243
- elif isinstance (o , uuid .UUID ):
244
- return _lrepr_uuid (o )
245
- else :
246
- return repr (o )
188
+ return o ._lrepr (
189
+ human_readable = human_readable ,
190
+ print_dup = print_dup ,
191
+ print_length = print_length ,
192
+ print_level = print_level ,
193
+ print_meta = print_meta ,
194
+ print_readably = print_readably ,
195
+ )
247
196
248
197
249
- @_lrepr_fallback .register (bool )
198
+ @lrepr .register (bool )
250
199
def _lrepr_bool (o : bool , ** _ ) -> str :
251
200
return repr (o ).lower ()
252
201
253
202
254
- @_lrepr_fallback .register (type (None ))
203
+ @lrepr .register (type (None ))
255
204
def _lrepr_nil (_ : None , ** __ ) -> str :
256
205
return "nil"
257
206
258
207
259
- @_lrepr_fallback .register (str )
208
+ @lrepr .register (str )
260
209
def _lrepr_str (
261
210
o : str , human_readable : bool = False , print_readably : bool = PRINT_READABLY , ** _
262
211
) -> str :
@@ -267,53 +216,53 @@ def _lrepr_str(
267
216
return f'"{ o .encode ("unicode_escape" ).decode ("utf-8" )} "'
268
217
269
218
270
- @_lrepr_fallback .register (list )
219
+ @lrepr .register (list )
271
220
def _lrepr_py_list (o : list , ** kwargs ) -> str :
272
221
return f"#py { seq_lrepr (o , '[' , ']' , ** kwargs )} "
273
222
274
223
275
- @_lrepr_fallback .register (dict )
224
+ @lrepr .register (dict )
276
225
def _lrepr_py_dict (o : dict , ** kwargs ) -> str :
277
226
return f"#py { map_lrepr (o .items , '{' , '}' , ** kwargs )} "
278
227
279
228
280
- @_lrepr_fallback .register (set )
229
+ @lrepr .register (set )
281
230
def _lrepr_py_set (o : set , ** kwargs ) -> str :
282
231
return f"#py { seq_lrepr (o , '#{' , '}' , ** kwargs )} "
283
232
284
233
285
- @_lrepr_fallback .register (tuple )
234
+ @lrepr .register (tuple )
286
235
def _lrepr_py_tuple (o : tuple , ** kwargs ) -> str :
287
236
return f"#py { seq_lrepr (o , '(' , ')' , ** kwargs )} "
288
237
289
238
290
- @_lrepr_fallback .register (complex )
239
+ @lrepr .register (complex )
291
240
def _lrepr_complex (o : complex , ** _ ) -> str :
292
241
return repr (o ).upper ()
293
242
294
243
295
- @_lrepr_fallback .register (datetime .datetime )
244
+ @lrepr .register (datetime .datetime )
296
245
def _lrepr_datetime (o : datetime .datetime , ** _ ) -> str :
297
246
return f'#inst "{ o .isoformat ()} "'
298
247
299
248
300
- @_lrepr_fallback .register (Decimal )
249
+ @lrepr .register (Decimal )
301
250
def _lrepr_decimal (o : Decimal , print_dup : bool = PRINT_DUP , ** _ ) -> str :
302
251
if print_dup :
303
252
return f"{ str (o )} M"
304
253
return str (o )
305
254
306
255
307
- @_lrepr_fallback .register (Fraction )
256
+ @lrepr .register (Fraction )
308
257
def _lrepr_fraction (o : Fraction , ** _ ) -> str :
309
258
return f"{ o .numerator } /{ o .denominator } "
310
259
311
260
312
- @_lrepr_fallback .register (type (re .compile ("" )))
261
+ @lrepr .register (type (re .compile ("" )))
313
262
def _lrepr_pattern (o : Pattern , ** _ ) -> str :
314
263
return f'#"{ o .pattern } "'
315
264
316
265
317
- @_lrepr_fallback .register (uuid .UUID )
266
+ @lrepr .register (uuid .UUID )
318
267
def _lrepr_uuid (o : uuid .UUID , ** _ ) -> str :
319
268
return f'#uuid "{ str (o )} "'
0 commit comments