2

When I import a function at the top of my file and later use it in a function, in which I reassign it later on, python complains about Unresolved reference '_', e.g.:

from django.utils.translation import ugettext_lazy as _

def test():
    msg = _('Message')  # <= Unresolved reference '_'

    instance, _ = Something.objects.get_or_create(...)

what's happening here? Why python says Unresolved reference when evaluating the function, when it is only reassigned later on the next line?

radoh
  • 4,554
  • 5
  • 30
  • 45
  • 1
    maybe do not use `_` ? Its generally used as "meh, dont care" resolver – Patrick Artner Oct 15 '19 at 15:17
  • 2
    @PatrickArtner yes that would fix it, but I'm interested in *why* it's happening here. Funnily enough, `_` is generally used for Django translations too, that's how I encountered the error :) – radoh Oct 15 '19 at 15:20

1 Answers1

1

This is all but a duplicate, but since PyCharm generates a different error message this will be an answer.

Since you assign to _ in your function, all uses of _ in that function refer to the local variable rather than to the global (the imported function). PyCharm recognizes this and can’t identify a type for it to have.

Davis Herring
  • 36,443
  • 4
  • 48
  • 76