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

functools.partial does not carry over type of ForeignKey #750

Closed
StefanBrand opened this issue Nov 12, 2021 · 1 comment
Closed

functools.partial does not carry over type of ForeignKey #750

StefanBrand opened this issue Nov 12, 2021 · 1 comment
Labels
bug Something isn't working

Comments

@StefanBrand
Copy link

StefanBrand commented Nov 12, 2021

Bug report

What's wrong

I have two models:

ProtectedForeignKey = partial(models.ForeignKey, on_delete=models.PROTECT)

class Model(models.Model):
    config = ProtectedForeignKey("Config")

class Config(models.Model):
    version = models.IntegerField()

model = Model.objects.get()
model.config.version  # "_GT" has no attribute "version"

Without partial, it works fine:

class Model(models.Model):
    config = models.ForeignKey("Config", on_delete=models.PROTECT)  # ForeignKey[Union[Config, Combinable], Config]

Edit: And casting works in 1.8.0 (but not in 1.9.0 due to #725)

class Model(models.Model):
    config = cast("Config", ProtectedForeignKey("Config"))

How it should be

partial should carry over the ForeignKey type.

System information

  • OS: Ubuntu 20.04
  • python version: 3.9.1
  • django version: 3.1.5
  • mypy version: 0.910
  • django-stubs version: 1.9.0
  • django-stubs-ext version:
@StefanBrand StefanBrand added the bug Something isn't working label Nov 12, 2021
@flaeppe
Copy link
Member

flaeppe commented Sep 23, 2023

ProtectedForeignKey = partial(models.ForeignKey, on_delete=models.PROTECT)

class Model(models.Model):
    config = ProtectedForeignKey("Config")

ForeignKey is generic, but in this situation having unbound arguments. That's why e.g. there's a _GT in the error message. That's why I'll close this off.

@flaeppe flaeppe closed this as not planned Won't fix, can't repro, duplicate, stale Sep 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Development

No branches or pull requests

2 participants