Repository: marshmallow-code/marshmallow

Issue:
Proposal: Add a class Meta option to prevent adding a class to the internal registry
## Purpose 

Implement a way to bypass marshmallow's internal class registry when it isn't necessary in order to optimize memory usage.


## Proposed API

```python
class MyNonRegisteredSchema(Schema):
    class Meta:
        register = False
```


Discussion:

Patch:
diff --git a/CHANGELOG.rst b/CHANGELOG.rst
index e1b90723..b632a083 100644
--- a/CHANGELOG.rst
+++ b/CHANGELOG.rst
@@ -4,6 +4,11 @@ Changelog
 3.0.0rc2 (unreleased)
 +++++++++++++++++++++
 
+Features:
+
+- Add ``register`` *class Meta* option to allow bypassing marshmallow's
+  internal class registry when memory usage is critical (:issue:`660`).
+
 Bug fixes:
 
 - Fix serializing dict-like objects with properties (:issue:`1060`).
diff --git a/marshmallow/schema.py b/marshmallow/schema.py
index fcbe8849..93167ba5 100644
--- a/marshmallow/schema.py
+++ b/marshmallow/schema.py
@@ -137,7 +137,7 @@ class SchemaMeta(type):
     # NOTE: self is the class object
     def __init__(self, name, bases, attrs):
         super(SchemaMeta, self).__init__(name, bases, attrs)
-        if name:
+        if name and self.opts.register:
             class_registry.register(name, self)
         self._hooks = self.resolve_hooks()
 
@@ -216,6 +216,7 @@ class SchemaOpts(object):
         self.load_only = getattr(meta, 'load_only', ())
         self.dump_only = getattr(meta, 'dump_only', ())
         self.unknown = getattr(meta, 'unknown', RAISE)
+        self.register = getattr(meta, 'register', True)
 
 
 class BaseSchema(base.SchemaABC):
@@ -329,6 +330,10 @@ class BaseSchema(base.SchemaABC):
         - ``dump_only``: Tuple or list of fields to exclude from deserialization
         - ``unknown``: Whether to exclude, include, or raise an error for unknown
             fields in the data. Use `EXCLUDE`, `INCLUDE` or `RAISE`.
+        - ``register``: Whether to register the `Schema` with marshmallow's internal
+            class registry. Must be `True` if you intend to refer to this `Schema`
+            by class name in `Nested` fields. Only set this to `False` when memory
+            usage is critical. Defaults to `True`.
         """
         pass
 


Test patch:
diff --git a/tests/test_registry.py b/tests/test_registry.py
index b54f5ebe..16234259 100644
--- a/tests/test_registry.py
+++ b/tests/test_registry.py
@@ -21,6 +21,36 @@ def test_serializer_has_class_registry():
     assert 'tests.test_registry.MySubSchema' in class_registry._registry
 
 
+def test_register_class_meta_option():
+    class UnregisteredSchema(Schema):
+        class Meta:
+            register = False
+
+    class RegisteredSchema(Schema):
+        class Meta:
+            register = True
+
+    class RegisteredOverrideSchema(UnregisteredSchema):
+        class Meta:
+            register = True
+
+    class UnregisteredOverrideSchema(RegisteredSchema):
+        class Meta:
+            register = False
+
+    assert 'UnregisteredSchema' not in class_registry._registry
+    assert 'tests.test_registry.UnregisteredSchema' not in class_registry._registry
+
+    assert 'RegisteredSchema' in class_registry._registry
+    assert 'tests.test_registry.RegisteredSchema' in class_registry._registry
+
+    assert 'RegisteredOverrideSchema' in class_registry._registry
+    assert 'tests.test_registry.RegisteredOverrideSchema' in class_registry._registry
+
+    assert 'UnregisteredOverrideSchema' not in class_registry._registry
+    assert 'tests.test_registry.UnregisteredOverrideSchema' not in class_registry._registry
+
+
 def test_serializer_class_registry_register_same_classname_different_module():
 
     reglen = len(class_registry._registry)