Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

global name 'BidirectionalMapping' is not defined #118

Closed
andreykryazhev opened this issue Nov 2, 2020 · 21 comments
Closed

global name 'BidirectionalMapping' is not defined #118

andreykryazhev opened this issue Nov 2, 2020 · 21 comments

Comments

@andreykryazhev
Copy link

Hi, we are using version bidict==0.18.0 and CPython 2.7.13 and got the following error here:

bidict/_abc.py in subclasshook at line 90

    def __subclasshook__(cls, C):  # noqa: N803 (argument name should be lowercase)
        """Check if *C* is a :class:`~collections.abc.Mapping`
        that also provides an ``inverse`` attribute,
        thus conforming to the :class:`BidirectionalMapping` interface,
        in which case it will be considered a (virtual) C
        even if it doesn't explicitly extend it.
        """
        that also provides an ``inverse`` attribute,
        thus conforming to the :class:`BidirectionalMapping` interface,
        in which case it will be considered a (virtual) C
        even if it doesn't explicitly extend it.
        """
        if cls is not BidirectionalMapping:  # lgtm [py/comparison-using-is]  # global name 'BidirectionalMapping' is not defined here
            return NotImplemented
        if not Mapping.__subclasshook__(C):
            return NotImplemented
        mro = getattr(C, '__mro__', None)
        if mro is None:  # Python 2 old-style class
        ...

C is a <type 'unicode'> here. So we simply pass unicode string to logger and such error raised. Looks like this happened because of the following line in python2.7/logging/init.py:

if (args and len(args) == 1 and isinstance(args[0], collections.Mapping)

Could anybody help with this issue? What we are doing wrong? Thanks in advance.

@jab
Copy link
Owner

jab commented Nov 2, 2020

Hi @andreykryazhev, the version of bidict you're using has a bug in its __subclasshook__ implementation that was fixed in v0.20.0 (see #111 for details), but v0.18.3 was the last version to support Python 2. Note: The "fix" I ended up choosing was to just remove BidirectionalMapping.__subclasshook__ altogether.

Over a year ago I announced that bidict would be joining the python3statement.org bandwagon and dropping support for Python 2. I therefore have not backported this fix to a Python 2-compatible release. I hope you are able to either upgrade to Python 3 along with the latest version of bidict, or remove BidrectionalMapping.__subclasshook__ as a workaround.

@jab jab closed this as completed Nov 2, 2020
@andreykryazhev
Copy link
Author

Thanks for the reply, @jab ! Looks like the only way for Python2.7 is using forked version of bidict.

@jab
Copy link
Owner

jab commented Nov 2, 2020

You’re welcome, and sorry for this bug! Are you able to update to Python 3 at some point? Then you can more easily benefit from other fixes and features that land upstream.

In the meantime, rather than fork bidict, are you able to monkey patch the subclasshook method out of the BidirectionalMapping class in your application?

@andreykryazhev
Copy link
Author

Unfortunately we cannot update to Python 3 yet, as for monkey patching - thank for suggestion! I suppose it is fit excellent for tests but not for production code but other developers may have different opinion. Thanks one more time!

@jab
Copy link
Owner

jab commented Nov 2, 2020

No promises for the future, but I've backported the removal of BidirectionalMapping.__subclasshook__ to a Python 2-compatible branch. Travis-CI is super backed up right now, but if it ever catches up, a Python 2-compatible v0.18.4 release with this fix just might hit PyPI so you don't have to fork bidict yourself for this.

@andreykryazhev
Copy link
Author

Many thanks for change, @jab! Any chance that it will be possible to install it from pypi?

@jab
Copy link
Owner

jab commented Nov 3, 2020

Please give https://pypi.org/project/bidict/0.18.4/ a try and let me know if it resolves your issue.

@jab
Copy link
Owner

jab commented Nov 3, 2020

By the way, I should have done this sooner, but I tried to reproduce the issue you reported based on your description and wasn't able to:

(venv27) jab@tsmbp /t/venv27> python

WARNING: Python 2.7 is not recommended. 
This version is included in macOS for compatibility with legacy software. 
Future versions of macOS will not include Python 2.7. 
Instead, it is recommended that you transition to using 'python3' from within Terminal.

Python 2.7.16 (default, Jun  5 2020, 22:59:21) 
[GCC 4.2.1 Compatible Apple LLVM 11.0.3 (clang-1103.0.29.20) (-macos10.15-objc- on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import bidict
>>> bidict.__version__
'0.18.0'
>>> import logging
>>> logging.basicConfig(level="INFO")
>>> logging.info(u"Trying to reproduce: unicode triggers the bug?")
INFO:root:Trying to reproduce: unicode triggers the bug?
>>> logging.info(u"Trying to reproduce: %s", u"unicode triggers the bug?")
INFO:root:Trying to reproduce: unicode triggers the bug?

Is there something different your code is actually doing?

The only difference from your description that I can see is that I used Python 2.7.16 rather than 2.7.13, since 2.7.16 is the only Python 2 version I still have handy, and it's likely that isn't the reason I couldn't reproduce. (By the way, can you at least upgrade to Python 2.7.18, since it's the last Python 2 release?)

@andreykryazhev
Copy link
Author

Actually, this is an issue from logs which happened on remote machines and I agree that possibly some environment issue may lead to this error too (looks like it is not reproduced so simply). Simply seen that such hook was removed from Py3 bidict version, thought it was made by purpose... and created this issue for Python2.

@jab
Copy link
Owner

jab commented Nov 3, 2020

Gotcha. Can you confirm if 0.18.4 fixes the issue for you when you get a chance? Hope so!

@andreykryazhev
Copy link
Author

Sure, I will notify any updates here (but change-delivery may take some time).

@jab
Copy link
Owner

jab commented Nov 9, 2020

Hey, @andreykryazhev, just checking if you've had a chance to confirm the fix yet. Hope bidict 0.18.4 fixed the issue for you!

@andreykryazhev
Copy link
Author

Hi, @jab. I saw you have deployed 0.18.4 on PyPi. I think the change will be possible to confirm in a week (or may be two in a worst case). The update is on the way to production-servers still...

@andreykryazhev
Copy link
Author

Sad, but finally the change was not included to current release (I do not decide this), so it will be deployed only in next release (in a ~3 months) or if somebody will encounter to this issue again (via patch).
Thank you very much for you help, @jab! Will post any news here but at this time I do not know when this will happen.

@jab
Copy link
Owner

jab commented Nov 18, 2020

Too bad you can't yet confirm the fix in 0.18.4 works for you, and thank you for the update! If you can, please do let me know if it fixes the issue for you once you're able to test 0.18.4. In the meantime, if you find a way to reproduce the issue in 0.18.3 that you can share, that could also be helpful (e.g. for adding a regression test). Thanks again!

@jab
Copy link
Owner

jab commented Feb 20, 2021

Hey @andreykryazhev, hope you’ve been well. Can you say yet whether this fixed your issue?

@andreykryazhev
Copy link
Author

andreykryazhev commented Feb 24, 2021

Hi, @jab! Release (with bidict library version update) will happen approx in a two weeks. At this time I can see via logs that issue still happen (on previous release) time to time (for example it happened yesterday).
Thus, in a two-three weeks I can provide an update result. Is it OK?
Thanks for your attention!

P.S.: sorry for delay with answer, there were a public holidays.

@jab
Copy link
Owner

jab commented Mar 25, 2021

No worries, @andreykryazhev! Any luck?

@andreykryazhev
Copy link
Author

Sorry, at this time I am really forgot to notify you, @jab. Last release was deployed approx two week ago and no single similar issue from that time) So I hope it will never arise)

@jab
Copy link
Owner

jab commented Mar 25, 2021

Great this appears to be fixed, thanks. Please let me know if you ever see otherwise, though with the changes I made, I don’t see how that could happen.

@andreykryazhev
Copy link
Author

Sure. And thank you very much one more time, @jab!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants