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

"from injector import Key" fails #144

Closed
garyrob opened this issue May 1, 2020 · 3 comments
Closed

"from injector import Key" fails #144

garyrob opened this issue May 1, 2020 · 3 comments

Comments

@garyrob
Copy link

garyrob commented May 1, 2020

The Keys section on the Terminology page contains the line:

from injector import Key

However, when I run that line in the terminal, I get an error: "ImportError: cannot import name 'Key' from 'injector'". Other imports such as

from injector import InstanceProvider

work fine in my setup.

@gimbo
Copy link

gimbo commented May 11, 2020

Looking at the Changelog, we have:

0.17.0 — Deprecated Key, SequenceKey and MappingKey – use real types or type aliases instead

and

0.18.0 — Removed previously deprecated Key, BindingKey, SequenceKey and MappingKey pseudo-types

Here's the commit where they were deprecated. And here's a commit where the README was updated.

So the advice is apparently to "use real types or type aliases instead". E.g perhaps something like this would suffice for the example on the Terminology page...?

from typing import NewType

Name = NewType('Name', int)
Description = NewType('Description', int)

?

I'm guessing (as a beginner trying to comprehend things) that since the key classes aren't ever instantiated (?) it doesn't matter what type we base our new type off? So I went for int here.

I'm hesitant about that conclusion however, as in that README-update commit, @jstasiak replaced

Configuration = Key('configuration')

with a real Configuration class, carrying some data — so maybe the deprecation of Key carries more semantic weight than I'm understanding here. But as I said, I'm new here so I don't really know, just trying to figure it out too. :-)

@XFrankly
Copy link

I think nobody to update the docs - _ - !

@jstasiak
Copy link
Collaborator

Thanks for the info, documentation updated in e686f36.

@gimbo Anything returned when you requested a Key instance before wasn't type-checked as the types were opaque to type checkers, there was no way to specify the return type of Injector.get() etc.. Now, since Key and family have been removed and NewType support has been added to replace Key, the types do matter – if you have

UserId = NewType('UserId', int)

and then try to do string-things with it

def __init__(self, user_id: UserId):
    user_id + ' something'

mypy will complain (and so should other type checkers).

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

4 participants