|
6 | 6 | import os
|
7 | 7 | import re
|
8 | 8 | import sys
|
| 9 | +import types |
9 | 10 | import warnings
|
10 | 11 |
|
11 | 12 | from ._itertools import always_iterable
|
@@ -190,25 +191,27 @@ def _find_macro(self, name):
|
190 | 191 | def _check_macro_definitions(self, definitions):
|
191 | 192 | """Ensure that every element of 'definitions' is valid."""
|
192 | 193 | for defn in definitions:
|
193 |
| - self._check_macro_definition(defn) |
| 194 | + self._check_macro_definition(*defn) |
194 | 195 |
|
195 | 196 | def _check_macro_definition(self, defn):
|
196 | 197 | """
|
197 | 198 | Raise a TypeError if defn is not valid.
|
198 | 199 |
|
199 | 200 | A valid definition is either a (name, value) 2-tuple or a (name,) tuple.
|
200 | 201 | """
|
201 |
| - valid = ( |
202 |
| - isinstance(defn, tuple) |
203 |
| - and (len(defn) in (1, 2) and (isinstance(defn[1], str) or defn[1] is None)) |
204 |
| - and isinstance(defn[0], str) |
205 |
| - ) |
206 |
| - if not valid: |
| 202 | + if not isinstance(defn, tuple) or not self._is_valid_macro(*defn): |
207 | 203 | raise TypeError(
|
208 | 204 | f"invalid macro definition '{defn}': "
|
209 | 205 | "must be tuple (string,), (string, string), or (string, None)"
|
210 | 206 | )
|
211 | 207 |
|
| 208 | + @staticmethod |
| 209 | + def _is_valid_macro(name, value=None): |
| 210 | + """ |
| 211 | + A valid macro is a ``name : str`` and a ``value : str | None``. |
| 212 | + """ |
| 213 | + return isinstance(name, str) and isinstance(value, (str, types.NoneType)) |
| 214 | + |
212 | 215 | # -- Bookkeeping methods -------------------------------------------
|
213 | 216 |
|
214 | 217 | def define_macro(self, name, value=None):
|
|
0 commit comments