r-richmond edited a comment on issue #14396:
URL: https://github.com/apache/airflow/issues/14396#issuecomment-785517704


   >Seems like we end up with ever compiicating interfaces for no benefit other 
than "newer" approach where we have simpler alternative.
   
   I'd argue that the dataclass interface is clearer than the dictionary 
interface and thus simpler to use, although perhaps not as simple to author.
   
   >Now, let me see how would you like to implement backward-compatibility of 
this (this is the more standard case of custom operator. This is just copy of 
what we have in our documentation:
   
   >What solution would be good here if we use Dataclasses, we want to keep 
backwards compatibility and we want to give users typehint for context.
   
   >Any idea how to do it short of converting the Dataclass to TypeDict before 
calling execute (which would defeat the purpose of having Dataclass in the 
first place).?
   
   First off great challenge :) , 2nd I'd probably replace my earlier 
suggestion with the following as well
   
   ```python
   
   @dataclass
   class Demo:
       id: str
       value_dc: int
       user_defined: Dict[str, Any] = field(default_factory=dict)
   
       def __getitem__(self, item):
           logging.warning(msg=f"dictionary interface getitem is deprecated; 
update this to use the dataclass interface or context.user_defined")
           if item in self.__dict__.keys():
               return self.__dict__[item]
           elif item in self.user_defined:
               return self.user_defined[item]
           else:
               raise KeyError
   
       def __setitem__(self, key: str, value):
           logging.warning(msg=f"dictionary interface setitem is deprecated; 
update this to use the dataclass interface or context.user_defined")
           if key in self.__dict__.keys():
               logging.warning(msg=f"updated {key} which is a field preset by 
context; if this is intentional please use the dataclass interface otherwise 
use context.user_defined")
               self.__dict__[key] = value
           else:
               self.user_defined[key] = value
   
       def keys(self):
           # added as an example to show how far we could go to have a 
non-breaking change for 2.1
           logging.warning(msg=f"dictionary interface keys is deprecated; 
update this to use the dataclass interface")
           temp = self.__dict__
           temp.update(self.user_defined)
           return temp
   
   
   d = Demo(id="long_id", value_dc=1337)
   print(d["id"])
   d["new"] = 3
   print(d["new"])
   print(d.keys())
   ``` 
   returns
   ```
   WARNING:root:dictionary interface is deprecated please update this to use 
the dataclass interface
   long_id
   ```
   
   Seems like this would allow us to go straight to dataclasses without 
changing any functions or operators anywhere. We could then remove the 
dictionary api at the next major version. 
   
   What do you think? Is there another use case that I'm not aware of that this 
won't work for?


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to