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

[BUG] TypeError at library import (Python 3.9.1) #49

Closed
ravaszf opened this issue Mar 2, 2022 · 17 comments
Closed

[BUG] TypeError at library import (Python 3.9.1) #49

ravaszf opened this issue Mar 2, 2022 · 17 comments
Labels
bug Something isn't working

Comments

@ravaszf
Copy link
Contributor

ravaszf commented Mar 2, 2022

Describe the bug
I'm new to Python/FastAPI, so it is very well possible that it's just me doing something wrong, but my code fails to compile as soon as I add the line to import either SingleTenantAzureAuthorizationCodeBearer or MultiTenantAzureAuthorizationCodeBearer and I'm getting this error: TypeError: unhashable type: 'list'

First I thought it's related to my already written code, so I've started a clean project and followed the tutorial in the documentation, but that also fails as soon as I add the import line. So I'm completely lost here.

I'm using Python 3.9.1 and FastAPI 0.74.1

To Reproduce
Add code line:
from fastapi_azure_auth import SingleTenantAzureAuthorizationCodeBearer

Stack trace

Traceback (most recent call last):
  File "c:\program files\python39\lib\runpy.py", line 197, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "c:\program files\python39\lib\runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "C:\Users\...\AppData\Roaming\Python\Python39\Scripts\uvicorn.exe\__main__.py", line 7, in <module>
  File "C:\Users\...\AppData\Roaming\Python\Python39\site-packages\click\core.py", line 1137, in __call__
    return self.main(*args, **kwargs)
  File "C:\Users\...\AppData\Roaming\Python\Python39\site-packages\click\core.py", line 1062, in main
    rv = self.invoke(ctx)
  File "C:\Users\...\AppData\Roaming\Python\Python39\site-packages\click\core.py", line 1404, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "C:\Users\...\AppData\Roaming\Python\Python39\site-packages\click\core.py", line 763, in invoke
    return __callback(*args, **kwargs)
  File "C:\Users\...\AppData\Roaming\Python\Python39\site-packages\uvicorn\main.py", line 435, in main
    run(app, **kwargs)
  File "C:\Users\...\AppData\Roaming\Python\Python39\site-packages\uvicorn\main.py", line 461, in run
    server.run()
  File "C:\Users\...\AppData\Roaming\Python\Python39\site-packages\uvicorn\server.py", line 60, in run
    return asyncio.run(self.serve(sockets=sockets))
  File "c:\program files\python39\lib\asyncio\runners.py", line 44, in run
    return loop.run_until_complete(main)
  File "c:\program files\python39\lib\asyncio\base_events.py", line 642, in run_until_complete
    return future.result()
  File "C:\Users\...\AppData\Roaming\Python\Python39\site-packages\uvicorn\server.py", line 67, in serve
    config.load()
  File "C:\Users\...\AppData\Roaming\Python\Python39\site-packages\uvicorn\config.py", line 458, in load
    self.loaded_app = import_from_string(self.app)
  File "C:\Users\...\AppData\Roaming\Python\Python39\site-packages\uvicorn\importer.py", line 21, in import_from_string
    module = importlib.import_module(module_str)
  File "c:\program files\python39\lib\importlib\__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
  File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 790, in exec_module
  File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
  File ".\main.py", line 7, in <module>
    from fastapi_azure_auth import SingleTenantAzureAuthorizationCodeBearer
  File "C:\Users\...\AppData\Roaming\Python\Python39\site-packages\fastapi_azure_auth\__init__.py", line 1, in <module>
    from fastapi_azure_auth.auth import (  # noqa: F401
  File "C:\Users\...\AppData\Roaming\Python\Python39\site-packages\fastapi_azure_auth\auth.py", line 20, in <module>
    class AzureAuthorizationCodeBearerBase(SecurityBase):
  File "C:\Users\...\AppData\Roaming\Python\Python39\site-packages\fastapi_azure_auth\auth.py", line 29, in AzureAuthorizationCodeBearerBase
    iss_callable: Optional[Callable[[str], Awaitable[str]]] = None,
  File "c:\program files\python39\lib\typing.py", line 262, in inner
    return func(*args, **kwds)
  File "c:\program files\python39\lib\typing.py", line 339, in __getitem__
    return self._getitem(self, parameters)
  File "c:\program files\python39\lib\typing.py", line 463, in Optional
    return Union[arg, type(None)]
  File "c:\program files\python39\lib\typing.py", line 262, in inner
    return func(*args, **kwds)
  File "c:\program files\python39\lib\typing.py", line 339, in __getitem__
    return self._getitem(self, parameters)
  File "c:\program files\python39\lib\typing.py", line 451, in Union
    parameters = _remove_dups_flatten(parameters)
  File "c:\program files\python39\lib\typing.py", line 231, in _remove_dups_flatten
    return tuple(_deduplicate(params))
  File "c:\program files\python39\lib\typing.py", line 205, in _deduplicate
    all_params = set(params)
TypeError: unhashable type: 'list'

Your configuration
Exactly the same as the tutorial here: https://intility.github.io/fastapi-azure-auth/single-tenant/fastapi_configuration

@ravaszf ravaszf added the question Further information is requested label Mar 2, 2022
@JonasKs
Copy link
Member

JonasKs commented Mar 2, 2022

Hi!
That’s weird. I’ll try to reproduce today or tomorrow.

Could you let me know which version of this library you use?

iss_callable: Optional[Callable[[str], Awaitable[str]]] = None Seems to be the issue. Reading this SO thread, an upgrade of Python 3.9 could be sufficient.

@JonasKs
Copy link
Member

JonasKs commented Mar 2, 2022

A pull request to change from collections to the typing library as suggested is welcome too, if that fixes the problem for you 😊

@ravaszf
Copy link
Contributor Author

ravaszf commented Mar 2, 2022

Thank you for the quick reply.

Right, I forgot to mention that I'm using the latest version of the library, 3.2.0.

I've tried to upgrade from Python 3.9.1 to 3.10.0, but that did not make any difference. Python 3.9.2 is not available for me due to company policies.

Then I changed from collections to typing and now the code compiles and I can authenticate successfully. Thanks for the suggestion.

@JonasKs
Copy link
Member

JonasKs commented Mar 2, 2022

Perfect! Would you like to submit a PR to fix it here as well? 😊

@ravaszf
Copy link
Contributor Author

ravaszf commented Mar 2, 2022

Bare in mind I'm a total noob here :) So not sure how to do that properly.

@JonasKs
Copy link
Member

JonasKs commented Mar 2, 2022

Well, I'd say that's a even better reason to make an attempt!

I'll write the steps here, if you want to try it out (and become a contributor), you'll have the opportunity now. These steps are the same for basically all open source projects. I'll be happy to answer any questions you might have! If you don't have time or don't want to, just let me know and I'll do it. 😊

Fork

First off, you need to fork the project. You do this by clicking this button:

image

You then go into your project, which should now exist under your own profile here.

Clone

Clone this project, either using https or ssh. If you haven't set up SSH keys, you chose https.

image

Copy this URL, and paste it in your terminal in the folder you'd like it to clone to:

git clone <url>

Example:
image

OR with ssh:

image

(Cloning projects can also be done through clients or editors, but I can't help you with that)

Install the project dependencies

Now, we have the project installed. If you have poetry, you can just type poetry install and it'll install the entire environment:

image

If you don't have poetry, you must install it first. There's the official way here, but if you're on a mac you can also just do a brew install poetry. pip install poetry should also work in most cases.

When you've written poetry install you can write poetry shell to activate the environment:

image

Pre-commit

This project uses pre-commit. It's not really documented anywhere, so I'll make sure to do that! How ever, all it requires is that you run pre-commit install. This will install the pre-commit hooks:
image

Changing branch and edit the code

Now, all we have to do is create a new branch (from the branch you're on) and edit the code:

git checkout -b fix/typing-error

image

And then, edit our code in your editor.

Now, we can write git status to see what files we've made changes to:

image

We then add this file to commit, by typing git add <filename>, or git add . to add them all. When you've added the file, you can create a commit. git commit -m "<commit message>" is the simplest way:

image

As you can see, the pre-commit hooks are now installing.

Making pre-commit hooks pass

With my changes, the pre-commit hook actually changed my file, and my commit did not go through:
image

Typing git status I can see that the file is changed from last time I add it:

image

Since the file was fixed, all I need to do is to add the file and try to commit again:

image

Success!

Pushing and creating a pull request

Now we push these changes into our branch by typing git push origin <branch name>:

image

Then, navigate back to this repository in the web browser and you'll see this button. Click it and create a pull request.

image

When you create the pull request, tests will automatically run in the branch. 😊

@ravaszf
Copy link
Contributor Author

ravaszf commented Mar 2, 2022

Thanks for the detailed guide, I'd definitely would like to give it a try, but won't have time today. I'd get back to it tomorrow or the day after, as time allows. Hope that works.

@JonasKs
Copy link
Member

JonasKs commented Mar 2, 2022

No rush at all! Let me know if you have any questions or bump into any issues. 😄

EDIT: I've now added a CONTRIBUTING.md file, which sums the comment up in a few steps.

@JonasKs JonasKs added bug Something isn't working and removed question Further information is requested labels Mar 3, 2022
@JonasKs JonasKs changed the title [BUG/Question] TypeError at library import [BUG] TypeError at library import (Python 3.9.1) Mar 3, 2022
@ravaszf
Copy link
Contributor Author

ravaszf commented Mar 3, 2022

Hi Jonas,

I've tried to contribute by following your guide, but ran into some issues with poetry. My company uses a proxy repo and one of the requirements of poetry is blocked, so I can't install it. Unfortunately I don1t have permissions to view the detailed report, so I don't know why is it blocked exactly, but apparently for now this is a no-go, on my company machine at least.

So I figured I'll try from my private PC, but that had nothing development related set up and got to the point where I've installed poetry with pip install. But then for pre-commit install I don't get any output. No error either, seemingly nothing happens:
kép

So not sure what's going on, but didn't want to proceed. Any ideas on that?

And another note regarding Python 3.10.0. I've made a rookie mistake and did not create venvs, and did not realize that after installing 3.10.0 my code was still running on 3.9.1. So it's actually possible that upgrading too would solve the issue. Unfortunately I can't verify as on 3.10.0 I can't install the dotenv package, due to above mentioned proxy repo blocking this as well, so I can't compile the code without rewriting it with an alternative to dotenv. Or finding the right team and getting dotenv unblocked. Neither of them would be quick.

@JonasKs
Copy link
Member

JonasKs commented Mar 3, 2022

I've tried to contribute by following your guide, but ran into some issues with poetry. My company uses a proxy repo and one of the requirements of poetry is blocked, so I can't install it. Unfortunately I don1t have permissions to view the detailed report, so I don't know why is it blocked exactly, but apparently for now this is a no-go, on my company machine at least.

Ah, dang.

So I figured I'll try from my private PC, but that had nothing development related set up and got to the point where I've installed poetry with pip install. But then for pre-commit install I don't get any output. No error either, seemingly nothing happens:

Hmm, what happens if you run pre-commit run --all-files?

And another note regarding Python 3.10.0. I've made a rookie mistake and did not create venvs, and did not realize that after installing 3.10.0 my code was still running on 3.9.1. So it's actually possible that upgrading too would solve the issue. Unfortunately I can't verify as on 3.10.0 I can't install the dotenv package, due to above mentioned proxy repo blocking this as well, so I can't compile the code without rewriting it with an alternative to dotenv. Or finding the right team and getting dotenv unblocked. Neither of them would be quick.

Sounds pretty rough to develop at your work environment, not gonna lie. 😁

@ravaszf
Copy link
Contributor Author

ravaszf commented Mar 3, 2022

I've tried to contribute by following your guide, but ran into some issues with poetry. My company uses a proxy repo and one of the requirements of poetry is blocked, so I can't install it. Unfortunately I don1t have permissions to view the detailed report, so I don't know why is it blocked exactly, but apparently for now this is a no-go, on my company machine at least.

Ah, dang.

So I figured I'll try from my private PC, but that had nothing development related set up and got to the point where I've installed poetry with pip install. But then for pre-commit install I don't get any output. No error either, seemingly nothing happens:

Hmm, what happens if you run pre-commit run --all-files?

Same, no output at all :/

And another note regarding Python 3.10.0. I've made a rookie mistake and did not create venvs, and did not realize that after installing 3.10.0 my code was still running on 3.9.1. So it's actually possible that upgrading too would solve the issue. Unfortunately I can't verify as on 3.10.0 I can't install the dotenv package, due to above mentioned proxy repo blocking this as well, so I can't compile the code without rewriting it with an alternative to dotenv. Or finding the right team and getting dotenv unblocked. Neither of them would be quick.

Sounds pretty rough to develop at your work environment, not gonna lie. 😁

Yeah, especially if one's not a developer, like myself :) Also, it's a fairly new tool, Both the tool and the processes around it are still tweaked, which also does not help much.

@JonasKs
Copy link
Member

JonasKs commented Mar 3, 2022

Hmm, that’s weird.

I’m not sure what’s wrong honestly. If you have poetry and then write poetry install, then poetry shell you should be in the environment, with pre-commit installed.

@ravaszf
Copy link
Contributor Author

ravaszf commented Mar 3, 2022

Yeah, weird. poetry install completed successfully, the venv shown by poetry shell looks a bit different than your's, but I think that's because I'm on Windows. Yet still nothing for pre-commit install:
kép

Btw, it was surprisingly easy to find someone with a way around the blocked dotenv package, so I was able to test Python 3.10.0 and with that it works even if I revert to collections.abc.

@JonasKs
Copy link
Member

JonasKs commented Mar 3, 2022

Yeah, I honestly don’t know what’s wrong 🤔

If you’d like to fix the bug and then submit a PR, it’s not the end of the world with linting. I can fix that in your PR 😄

@ravaszf
Copy link
Contributor Author

ravaszf commented Mar 7, 2022

Unfortunately I could not figure out what's wrong with poetry, but I've created a new PR.

@JonasKs
Copy link
Member

JonasKs commented Mar 7, 2022

Thank you! 😄 I'll fix whatever needs fixing and push a new version to PyPi ASAP.

@JonasKs
Copy link
Member

JonasKs commented Mar 7, 2022

@ravaszf , I've pushed a change to PyPi.

Thank you for your bug report and contribution! Let me know if there's anything more I can help you with.

EDIT: You've also been tagged as a contributor if you look at the release here. We also appreciate a ⭐ on the project if you haven't done so 😊

@JonasKs JonasKs closed this as completed Mar 7, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants