Contributor
This change adds feature-parity with node-sass importers, where callbacks are passed both the value of the import directive and the path from which the directive was made. This is particularly useful in cases where the importer needs to resolve relative paths.
I wasn't sure about the best way to go about maintaining backwards-compatibility. Here I added a conditional getargspec call in the importer callback wrapper, but I'm open to any other suggestions.
Member
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Contributor Author
@asottile I've come to regret the getargspec here a bit. Specifically, it doesn't work with either __call__ or with an instance of functools.partial. Would you be open to a PR that had something like:
def _importer_callback_wrapper(func): num_args = None @functools.wraps(func) def inner(path, prev): path, prev = path.decode('UTF-8'), prev.decode('UTF-8') if num_args is None: num_args = 2 try: ret = func(path, prev) except TypeError: num_args = 1 ret = func(path) elif num_args == 2: ret = func(path, prev) else: ret = func(path) return _normalize_importer_return_value(ret) return inner
Member
Ah yeah that limitation frequently bites
Maybe just (typed on phone sorry):
try: ret = func(path, prev) except TypeError as e: if not ... str(e): # test to make sure it is the TypeError we expect raise ret = func(path) return ...
Contributor Author
I'm fine with that. The only tricky thing is that the message looks different in python 2 and 3. In python 2 it's
TypeError('foo() takes exactly X argument[s] (2 given)')
whereas in python 3 it's:
TypeError('foo() takes X positional argument[s] but 2 were given')
So I guess we could do something like:
try: ret = func(path, prev) except TypeError as e: msg = str(e) if not msg.endswith('(2 given)') and not msg.endswith('but 2 were given'): raise ret = func(path)
Member
Oof, probably fine to just assume all TypeErrors are arg count errors then and drop the check
Merged