# Buggy code
def __init__(self, id, filename=None, index=None):
    self.field_data = YTFieldData()
    self.field_parameters = {}
    self.id = id
    self._child_mask = self._child_indices = self._child_index_mask = None
    self.ds = index.dataset
    self._index = weakref.proxy(index)
    self.start_index = None
    self.filename = filename
    self._last_mask = None
    self._last_count = -1
    self._last_selector_id = None
    self._current_particle_type = "all"
    self._current_fluid_type = self.ds.default_fluid_type


# Traceback (TypeError)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-16-ea26250246dd> in <module>()
----> 1 grid['particle_velocity_magnitude']

/home/ychen/src/yt-git/yt/data_objects/grid_patch.py in __getitem__(self, key)
93
94     def __getitem__(self, key):
---> 95         tr = super(AMRGridPatch, self).__getitem__(key)
96         try:
97             fields = self._determine_fields(key)

/home/ychen/src/yt-git/yt/data_objects/data_containers.py in __getitem__(self, key)
279                 return self.field_data[f]
280             else:
--> 281                 self.get_data(f)
282         # fi.units is the unit expression string. We depend on the registry
283         # hanging off the dataset to define this unit object.

/home/ychen/src/yt-git/yt/data_objects/data_containers.py in get_data(self, fields)
1334
1335         fields_to_generate += gen_fluids + gen_particles
-> 1336         self._generate_fields(fields_to_generate)
1337         for field in list(self.field_data.keys()):
1338             if field not in ofields:

/home/ychen/src/yt-git/yt/data_objects/data_containers.py in _generate_fields(self, fields_to_generate)
1354                 fi = self.ds._get_field_info(*field)
1355                 try:
-> 1356                     fd = self._generate_field(field)
1357                     if fd is None:
1358                         raise RuntimeError

/home/ychen/src/yt-git/yt/data_objects/data_containers.py in _generate_field(self, field)
314                 tr = self._generate_container_field(field)
315             if finfo.particle_type: # This is a property now
--> 316                 tr = self._generate_particle_field(field)
317             else:
318                 tr = self._generate_fluid_field(field)

/home/ychen/src/yt-git/yt/data_objects/data_containers.py in _generate_particle_field(self, field)
399         else:
400             with self._field_type_state(ftype, finfo, gen_obj):
--> 401                 rv = self.ds._get_field_info(*field)(gen_obj)
402         return rv
403

/home/ychen/src/yt-git/yt/fields/derived_field.py in __call__(self, data)
235                 "for %s" % (self.name,))
236         with self.unit_registry(data):
--> 237             dd = self._function(self, data)
238         for field_name in data.keys():
239             if field_name not in original_fields:

/home/ychen/src/yt-git/yt/fields/particle_fields.py in _particle_velocity_magnitude(field, data)
282         return np.sqrt((data[ptype, svel % 'x'] - bulk_velocity[0])**2
283                      + (data[ptype, svel % 'y'] - bulk_velocity[1])**2
--> 284                      + (data[ptype, svel % 'z'] - bulk_velocity[2])**2 )
285
286     registry.add_field((ptype, "particle_velocity_magnitude"),

TypeError: 'NoneType' object is not subscriptable

# Fixed code
def __init__(self, id, filename=None, index=None):
    super(AMRGridPatch, self).__init__(index.dataset, None)
    self.id = id
    self._child_mask = self._child_indices = self._child_index_mask = None
    self.ds = index.dataset
    self._index = weakref.proxy(index)
    self.start_index = None
    self.filename = filename
    self._last_mask = None
    self._last_count = -1
    self._last_selector_id = None