Skip to content

Access function to be pickled as attribute, not method, to avoid error.#8823

Merged
ashb merged 4 commits intoapache:masterfrom
astronomer:fix-function-pickle-problems
May 12, 2020
Merged

Access function to be pickled as attribute, not method, to avoid error.#8823
ashb merged 4 commits intoapache:masterfrom
astronomer:fix-function-pickle-problems

Conversation

@jhtimmins
Copy link
Copy Markdown
Contributor

Currently, a static function is getting saved to DagFileProcessorAgent as an instance attribute, so it gets treated like an instance method when pickled, causing an error. By using getattr to access the attribute we can avoid this issue.

Related to issue #8674

Make sure to mark the boxes below before creating PR: [x]

  • Description above provides context of the change
  • Unit tests coverage for changes (not needed for documentation changes)
  • Target Github ISSUE in description if exists
  • Commits follow "How to write a good git commit message"
  • Relevant documentation is updated including usage instructions.
  • I will engage committers as explained in Contribution Workflow Example.

In case of fundamental code change, Airflow Improvement Proposal (AIP) is needed.
In case of a new dependency, check compliance with the ASF 3rd Party License Policy.
In case of backwards incompatible changes please leave a note in UPDATING.md.
Read the Pull Request Guidelines for more information.

@boring-cyborg boring-cyborg bot added the area:Scheduler including HA (high availability) scheduler label May 11, 2020
self._processor_factory,
# getattr allows pickling by making _processor_factory get
# treated as a function, not an instance method.
getattr(self, "_processor_factory"),
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LOLwhat. Thanks pickle.

Is _processor_factory marked as @staticmethod? If not it should be, and then probably this becomes

Suggested change
getattr(self, "_processor_factory"),
type(self)._processor_factory,

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ashb I agree, for purposes of consistency. But I actually think getattr is more clear. IMO type() makes it look like it's a class attribute, which isn't the case.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, that's not true. Apparently pylint expects type(self) to access an explicitly declared class attribute, whereas getattr has no such requirement. Since _processor_factory is declared at runtime, and is really a dynamic instance attribute, I think it makes sense to stay with getattr.

Also, I tried declaring _processor_factory at the class level and using type(self)._processor_factory, but it lead to an error in the child process. This didn't seem worth debugging just to use type(self).

@ashb ashb merged commit 7533378 into apache:master May 12, 2020
@kaxil kaxil deleted the fix-function-pickle-problems branch May 12, 2020 10:10
kaxil pushed a commit that referenced this pull request Jun 30, 2020
…r. (#8823)

* Access function to be pickled as attribute, not method, to avoid error.

* Access type attribute to allow pickling.

* Use getattr instead of type(self) to fix linting error.

(cherry picked from commit 7533378)
@kaxil kaxil added this to the Airflow 1.10.11 milestone Jun 30, 2020
kaxil pushed a commit that referenced this pull request Jul 1, 2020
…r. (#8823)

* Access function to be pickled as attribute, not method, to avoid error.

* Access type attribute to allow pickling.

* Use getattr instead of type(self) to fix linting error.

(cherry picked from commit 7533378)
cfei18 pushed a commit to cfei18/incubator-airflow that referenced this pull request Mar 5, 2021
…r. (apache#8823)

* Access function to be pickled as attribute, not method, to avoid error.

* Access type attribute to allow pickling.

* Use getattr instead of type(self) to fix linting error.

(cherry picked from commit 7533378)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:Scheduler including HA (high availability) scheduler

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants